Automatically bundle application into double-clickable .app

Alec Jacobson

September 30, 2013

weblog/

Here's a bash script to automatically bundle up your executable program into a "double-clickable" .app application directory. This works nicely for making GLUT applications look a bit more authentic. Save the following in a file called build_app:

#!/bin/bash

# based on https://raw.github.com/ixchow/Preshack/master/Basis/osx/build_app.pl
if [ "$#" -eq 0 ]
then
  USAGE="Usage:
  build_app.sh path/to/executable path/to/icon.icns path/to/new.app
or
  build_app.sh path/to/executable path/to/icon.icns
or
  build_app.sh path/to/executable
";
  echo "$USAGE";
  exit 1
fi

BIN="$1"

if [ "$#" -ge 2 ]
then
  ICNS="$2"
else
  ICNS=""
fi

if [ "$#" -ge 3 ]
then
  APP="$3"
else
  APP="$1.app"
fi


NAME=`basename $1`
SHORT=${NAME:0:4}

mkdir -p "$APP/Contents/Resources" || exit 1;
mkdir -p "$APP/Contents/MacOS" || exit 1;

PLIST="$APP/Contents/Info.plist";

echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>English</string>
    <key>CFBundleExecutable</key>
    <string>$NAME</string>
    <key>CFBundleIconFile</key>
    <string>$NAME.icns</string>
    <key>CFBundleIdentifier</key>
    <string>com.tchow.$NAME</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleNAME</key>
    <string>$NAME</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleSignature</key>
    <string>$SHORT</string>
    <key>CFBundleVersion</key>
    <string>1.0</string>
</dict>
</plist>" > $PLIST
cp $BIN $APP/Contents/MacOS/$NAME
if [ ! -z "$ICNS" ]
then
  cp $ICNS $APP/Contents/Resources/$NAME.icns
fi

Then you can run it on you executable like this:

build_app executable icon.icns output.app

Source