]> arthur.barton.de Git - appify.git/blobdiff - appify.sh
New option to add an icon to the application bundle
[appify.git] / appify.sh
old mode 100644 (file)
new mode 100755 (executable)
index 55052f8..c7ffa8e
--- a/appify.sh
+++ b/appify.sh
@@ -1,41 +1,83 @@
 #!/bin/bash
-
 #
-# appify -- convert your non-interactive shell script into a Mac OS X application
+# appify -- convert non-interactive shell script into Mac OS X applications
 # Copyright (C) 2010  Adam Backstrom
-# 
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
-# 
+# Copyright (C) 2015  Alexander Barton <alex@barton.de>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
-# 
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 #
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+args=$(getopt h $*)
+
+function usage {
+    echo "Usage: $0 [-h] <script.sh> <target.app> [<icons.icns>]"
+    exit 2
+}
+
+if [ $? != 0 ]; then
+    usage
+fi
+
+set -- $args
+for i ; do
+    case "$i"
+    in
+        -h) usage ; shift ;;
+        --) shift ; break ;;
+    esac
+done
+
+if [ $# -lt 2 -o $# -gt 3 ]; then
+    usage
+fi
 
 SCRIPT=$1
 TARGET=$2
+ICON=$3
+
+BASENAME=`basename "$SCRIPT"`
+
+if [ ! -r "$SCRIPT" ]; then
+    echo "$SCRIPT isn't readable!" 1>&2
+    exit 3
+fi
 
-if [ -d "$TARGET" ]; then
-    echo "$TARGET exists, exiting" 1>&2
-    exit 1
+if [ -e "$TARGET" ]; then
+    echo "$TARGET exists!" 1>&2
+    exit 3
+fi
+
+if [ -n "$ICON" -a ! -r "$ICON" ]; then
+    echo "$ICON isn't readable!" 1>&2
+    exit 3
 fi
 
 SCRIPTSIZE=$(ls -l "$SCRIPT" | awk '{print $5}')
 
 if [ $SCRIPTSIZE -lt 28 ]; then
-    echo -e "Please pad your script to at least 28 bytes; Mac OS X will not\nrecognize scripts that are smaller than this value." 1>&2
-    exit 2
+    echo -e "Script smaller than size allowed by OS. Please pad to 28 characters." 1>&2
+    exit 4
 fi
 
-mkdir -p "$TARGET/Contents/MacOS"
-mkdir -p "$TARGET/Contents/Resources"
+#
+# done checking args; create the app
+#
+
+umask 0022
+
+mkdir -p "$TARGET/Contents/MacOS" || exit 1
+mkdir -p "$TARGET/Contents/Resources" || exit 1
 
 cat <<EOF >"$TARGET/Contents/Info.plist"
 <?xml version="1.0" encoding="UTF-8"?>
@@ -43,7 +85,7 @@ cat <<EOF >"$TARGET/Contents/Info.plist"
 <plist version="1.0">
 <dict>
     <key>CFBundleExecutable</key>
-    <string>run.sh</string>
+    <string>${BASENAME}</string>
     <key>CFBundleIconFile</key>
     <string></string>
     <key>CFBundleInfoDictionaryVersion</key>
@@ -54,9 +96,20 @@ cat <<EOF >"$TARGET/Contents/Info.plist"
     <string>????</string>
     <key>CFBundleVersion</key>
     <string>1.0</string>
+EOF
+
+if [ -n "$ICON" ]; then
+    cat <<EOF >>"$TARGET/Contents/Info.plist"
+    <key>CFBundleIconFile</key>
+    <string>app.icns</string>
+EOF
+    cp "$ICON" "$TARGET/Contents/Resources/app.icns" || exit 1
+fi
+
+cat <<EOF >>"$TARGET/Contents/Info.plist"
 </dict>
 </plist>
 EOF
 
-cp "$SCRIPT" "$TARGET/Contents/MacOS/run.sh"
-chmod 755 "$TARGET/Contents/MacOS/run.sh"
+cp "$SCRIPT" "$TARGET/Contents/MacOS/$BASENAME" || exit 1
+chmod 755 "$TARGET/Contents/MacOS/$BASENAME" || exit 1