]> arthur.barton.de Git - appify.git/blob - appify.sh
New option to add an icon to the application bundle
[appify.git] / appify.sh
1 #!/bin/bash
2 #
3 # appify -- convert non-interactive shell script into Mac OS X applications
4 # Copyright (C) 2010  Adam Backstrom
5 # Copyright (C) 2015  Alexander Barton <alex@barton.de>
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with this program; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 args=$(getopt h $*)
22
23 function usage {
24     echo "Usage: $0 [-h] <script.sh> <target.app> [<icons.icns>]"
25     exit 2
26 }
27
28 if [ $? != 0 ]; then
29     usage
30 fi
31
32 set -- $args
33 for i ; do
34     case "$i"
35     in
36         -h) usage ; shift ;;
37         --) shift ; break ;;
38     esac
39 done
40
41 if [ $# -lt 2 -o $# -gt 3 ]; then
42     usage
43 fi
44
45 SCRIPT=$1
46 TARGET=$2
47 ICON=$3
48
49 BASENAME=`basename "$SCRIPT"`
50
51 if [ ! -r "$SCRIPT" ]; then
52     echo "$SCRIPT isn't readable!" 1>&2
53     exit 3
54 fi
55
56 if [ -e "$TARGET" ]; then
57     echo "$TARGET exists!" 1>&2
58     exit 3
59 fi
60
61 if [ -n "$ICON" -a ! -r "$ICON" ]; then
62     echo "$ICON isn't readable!" 1>&2
63     exit 3
64 fi
65
66 SCRIPTSIZE=$(ls -l "$SCRIPT" | awk '{print $5}')
67
68 if [ $SCRIPTSIZE -lt 28 ]; then
69     echo -e "Script smaller than size allowed by OS. Please pad to 28 characters." 1>&2
70     exit 4
71 fi
72
73 #
74 # done checking args; create the app
75 #
76
77 umask 0022
78
79 mkdir -p "$TARGET/Contents/MacOS" || exit 1
80 mkdir -p "$TARGET/Contents/Resources" || exit 1
81
82 cat <<EOF >"$TARGET/Contents/Info.plist"
83 <?xml version="1.0" encoding="UTF-8"?>
84 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
85 <plist version="1.0">
86 <dict>
87     <key>CFBundleExecutable</key>
88     <string>${BASENAME}</string>
89     <key>CFBundleIconFile</key>
90     <string></string>
91     <key>CFBundleInfoDictionaryVersion</key>
92     <string>1.0</string>
93     <key>CFBundlePackageType</key>
94     <string>APPL</string>
95     <key>CFBundleSignature</key>
96     <string>????</string>
97     <key>CFBundleVersion</key>
98     <string>1.0</string>
99 EOF
100
101 if [ -n "$ICON" ]; then
102     cat <<EOF >>"$TARGET/Contents/Info.plist"
103     <key>CFBundleIconFile</key>
104     <string>app.icns</string>
105 EOF
106     cp "$ICON" "$TARGET/Contents/Resources/app.icns" || exit 1
107 fi
108
109 cat <<EOF >>"$TARGET/Contents/Info.plist"
110 </dict>
111 </plist>
112 EOF
113
114 cp "$SCRIPT" "$TARGET/Contents/MacOS/$BASENAME" || exit 1
115 chmod 755 "$TARGET/Contents/MacOS/$BASENAME" || exit 1