Skip to content

Instantly share code, notes, and snippets.

@oubiwann
Forked from advorak/appify.sh
Last active August 13, 2024 01:24
Show Gist options
  • Save oubiwann/453744744da1141ccc542ff75b47e0cf to your computer and use it in GitHub Desktop.
Save oubiwann/453744744da1141ccc542ff75b47e0cf to your computer and use it in GitHub Desktop.
appify — create the simplest possible Mac app from a shell script (adds an application icon)
#!/usr/bin/env bash
VERSION=4.0.1
SCRIPT=`basename "$0"`
APPNAME="My App"
APPICONS="/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/GenericApplicationIcon.icns"
OSX_VERSION=`sw_vers -productVersion`
PWD=`pwd`
function usage {
cat <<EOF
$SCRIPT v${VERSION} for for Mac OS X - https://gist.github.com/oubiwann/453744744da1141ccc542ff75b47e0cf
Usage:
$SCRIPT [options]
Options:
-h, --help Prints this help message, then exits
-s, --script Name of the script to 'appify' (required)
-n, --name Name of the application (default "$APPNAME")
-i, --icons Name of the icons file to use when creating the app
(defaults to $APPICONS)
-v, --version Prints the version of this script, then exits
Description:
Creates the simplest possible Mac app from a shell script.
Appify has one required parameter, the script to appify:
$SCRIPT --script my-app-script.sh
Note that you cannot rename appified apps. If you want to give your app
a custom name, use the '--name' option
$SCRIPT --script my-app-script.sh --name "Sweet"
Copyright:
Copyright (c) Thomas Aylott <http://subtlegradient.com/>
Modified by Mathias Bynens <http://mathiasbynens.be/>
Modified by Andrew Dvorak <http://OhReally.net/>
Rewritten by Duncan McGreggor <http://github.com/oubiwann/>
EOF
exit 1
}
function version {
echo "v${VERSION}"
exit 1
}
function error {
echo
echo "ERROR: $1"
echo
usage
}
while :; do
case $1 in
-h | --help ) usage;;
-s | --script ) APPSCRIPT="$2"; shift ;;
-n | --name ) APPNAME="$2"; shift ;;
-i | --icons ) APPICONS="$2"; shift ;;
-v | --version ) version;;
-- ) shift; break ;;
* ) break ;;
esac
shift
done
if [ -z ${APPSCRIPT+nil} ]; then
error "the script to appify must be provided!"
fi
if [ ! -f "$APPSCRIPT" ]; then
error "the can't find the script '$APPSCRIPT'"
fi
if [ -a "$APPNAME.app" ]; then
error "the bundle '$PWD/$APPNAME.app' already exists"
fi
APPDIR="$APPNAME.app/Contents"
mkdir -vp "$APPDIR"/{MacOS,Resources}
cp -v "$APPICONS" "$APPDIR/Resources/$APPNAME.icns"
cp -v "$APPSCRIPT" "$APPDIR/MacOS/$APPNAME"
chmod +x "$APPDIR/MacOS/$APPNAME"
cat <<EOF > "$APPDIR/Info.plist"
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleExecutable</key>
<string>$APPNAME</string>
<key>CFBundleGetInfoString</key>
<string>$APPNAME</string>
<key>CFBundleIconFile</key>
<string>$APPNAME</string>
<key>CFBundleName</key>
<string>$APPNAME</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleSignature</key>
<string>4242</string>
</dict>
</plist>
EOF
echo "Application bundle created at '$PWD/$APPNAME.app'"
echo
@pablonosh
Copy link

pablonosh commented Jul 24, 2018

I get

"The application cannot be opened because it has an incorrect executable format." on High Sierra.

You can’t open the application “iPhone Bitrise Build” because it is not supported on this type of Mac.

@dlpigpen
Copy link

The icon does not update if I replace by new one?

@imanassypov
Copy link

What needs to be done for the new app to be disoverable by system_profiler, as in 'system_profiler SPApplicationsDataType' ?
Is there a way to 'register' the newly packaged app so it shows as 'installed'?

@oubiwann
Copy link
Author

Yikes, I had no idea people were leaving comments here until today -- sorry, all!

I haven't used the script since 2016; I can give it a shot, and see if it still works.

@dlpigpen: usually running touch Your.app refreshes icons in Mac OS X ...

@b3z
Copy link

b3z commented May 6, 2021

All Bundles I create on bigSure are not compatible it says.

@yogasw
Copy link

yogasw commented May 20, 2021

@varenc
Copy link

varenc commented Dec 28, 2021

This worked great for turning a script into a .app! But if you're doing this to deal with permission stuff, I'll note that the .app still isn't treated like one by macOS for security permission purposes. For example, if your script needs the Accessibility permission, macOS asks you to grant Accessibility permissions to bash (or zsh/sh/env/whatever), even though it's run from this .app. The heavier-weight Platypus app, linked above, addresses this for me so that the .app itself is what needs the permissions. Though I imagine for many this isn't important.

@denwald
Copy link

denwald commented Jul 28, 2022

Thank you for this convenient script. It works like a charm on my Intel iMac running Monterey. On my MacBook Air M1 (Apple Silicon) on the other hand I only get a pop up which offers to install Rosetta emulation. This does not make a lot of sense to me, since I've only packaged a bash script. Any idea if this is a general limitation of macOS and the M1 SOC?

@bikeridercz
Copy link

bikeridercz commented May 13, 2024

Hi ! Something has to change over the time. The script successfully creates an .APP but it does not work in these days. I tried to make simple shell script that works when if invoked directly, however it does not work if wrapped by .APP. Any idea ?

radim@Radim-MacBookPro14 % ./test         
Ahoj !

radim@Radim-MacBookPro14 % ./test.app/Contents/MacOS/test 
Ahoj !

radim@Radim-MacBookPro14 % ./test.app 
zsh: permission denied: ./test.app

radim@Radim-MacBookPro14 % open ./test.app
The application cannot be opened for an unexpected reason, error=Error Domain=NSOSStatusErrorDomain Code=-10661 "(null)" UserInfo={_LSLine=4129, _LSFunction=_LSOpenStuffCallLocal}

radim@Radim-MacBookPro14 % sw_vers
ProductName:		macOS
ProductVersion:		14.4.1
BuildVersion:		23E224

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment