Skip to content

Instantly share code, notes, and snippets.

@golimpio
Last active February 12, 2024 20:27
Show Gist options
  • Save golimpio/9359041 to your computer and use it in GitHub Desktop.
Save golimpio/9359041 to your computer and use it in GitHub Desktop.
A shell script to create application shortcuts in Google Chrome on a Mac
#!/bin/sh
#----------------------------------------------------------------------------------------------------------
#
# Original author: Bracken King
# https://www.lessannoyingcrm.com/blog/2010/08/149/create+application+shortcuts+in+google+chrome+on+a+mac
#
# Modified by: Gilberto R. Olimpio
#
# Changes:
#
# [2020-07-03]
# - help message (-h | --help)
# - option to specify the script parameters via command line arguments
# - exit if icon image file does not exist
# - show error messages
# - accept variables and expand '~' for the icon path (e.g. ~/Pictures/icon.png)
# - confirm before creating the application
# - allow spaces in the application name
# - display information about the step being executed
#
#----------------------------------------------------------------------------------------------------------
red=`tput setaf 1`
yellow=`tput setaf 3`
blue=`tput setaf 4`
reset=`tput sgr0`
function error {
echo "${red}$1${reset}"
}
function info {
echo "${blue}$1${reset}"
}
function warn {
echo "${yellow}$1${reset}"
}
function check_icon_path {
eval icon=$icon
if [ ! -f "$icon" ]; then
error "Error: Image not found ($icon)"
false
else
true
fi
}
function parse_args {
while [ $# -gt 0 ]; do
case "$1" in
-h|--help)
echo "Create application shortcuts in Google Chrome on a Mac."
echo " "
echo "Usage:"
echo " create_desktop_app_google_chrome.sh [options]"
echo " "
echo "Options:"
echo " -h, --help : show this help message"
echo " -n, --name <name> : application name"
echo " -u, --url <url> : application URL url (e.g. https://calendar.google.com)"
echo " -i, --icon <path> : full path to the application icon image"
echo " -q, --no-confirmation : do not ask for confirmation before creating the application"
exit 0
;;
-n|--name)
shift
if test $# -gt 0; then
name=$1
else
error "Error: application <name> not specified"
exit 1
fi
shift
;;
-u|--url)
shift
if test $# -gt 0; then
url=$1
else
error "Error: application <url> not specified"
exit 1
fi
shift
;;
-i|--icon)
shift
if test $# -gt 0; then
icon=$1
if ! check_icon_path; then exit 3; fi
else
error "Error: icon <path> not specified"
exit 1
fi
shift
;;
-q|--no-confirmation)
no_confirmation="Y"
shift
;;
*)
error "Invalid argument: $1"
exit 2
;;
esac
done
}
function prompt {
echo $1
printf "> "
read inputline
}
parse_args "$@"
if [ -z "$name" ]; then
prompt "What should the Application be called?"
name=$inputline
echo " "
fi
if [ -z "$url" ]; then
prompt "What is the url (e.g. https://www.google.com/calendar/render)?"
url=$inputline
echo " "
fi
if [ -z "$icon" ]; then
while :; do
prompt "What is the full path to the icon (e.g. ~/Desktop/icon.png)?"
icon=$inputline
if check_icon_path; then
echo " "
break
fi
echo " "
done
fi
chromePath="/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
appRoot="$HOME/Applications"
# various paths used when creating the app
resourcePath="$appRoot/$name.app/Contents/Resources"
execPath="$appRoot/$name.app/Contents/MacOS"
profilePath="$appRoot/$name.app/Contents/Profile"
profileFirstRun="$profilePath/First Run"
plistPath="$appRoot/$name.app/Contents/Info.plist"
echo "Application parameters:"
echo " - name: $name"
echo " - url: $url"
echo " - icon: $icon"
echo " "
echo "Directories used when creating the Application:"
echo " - resourcePath: $resourcePath"
echo " - execPath: $execPath"
echo " - profilePath: $profilePath"
echo " - plistPath: $plistPath"
echo " "
if [ -z "$no_confirmation" ]; then
prompt "Do you want to continue and create the Application <y/n>? "
case $inputline in
n|N)
echo " "
warn "Cancelled!"
exit 0
;;
esac
fi
# make the directories
info "Creating directories..."
mkdir -p "$resourcePath" "$execPath" "$profilePath"
# this will disable the welcome dialog (when open the app for the first time)
touch "$profileFirstRun"
# convert the icon and copy into Resources
info "Converting icon file:"
info " [$resourcePath/icon.tiff]"
sips -s format tiff $icon --out "$resourcePath/icon.tiff" --resampleHeightWidth 128 128 >& /dev/null
tiff2icns -noLarge "$resourcePath/icon.tiff" >& /dev/null
# create the executable
info "Creating application executable:"
info " [$execPath/$name]"
cat >"$execPath/$name" <<EOF
#!/bin/sh
exec $chromePath --app="$url" --user-data-dir="$profilePath" "\$@"
EOF
chmod +x "$execPath/$name"
# create the Info.plist
info "Creating information property list:"
info " [$plistPath]"
cat > "$plistPath" <<EOF
<?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>$name</string>
<key>CFBundleIconFile</key>
<string>icon</string>
</dict>
</plist>
EOF
printf "\nDone!\n\n"
@kevnk
Copy link

kevnk commented Jun 17, 2020

This script doesn't work (probably because it's so old), but this one works: https://gist.github.com/demonbane/1065791

@golimpio
Copy link
Author

This script doesn't work (probably because it's so old), but this one works: https://gist.github.com/demonbane/1065791

Thanks for the update!

@golimpio
Copy link
Author

golimpio commented Jul 4, 2020

Gist has been updated and I've added a few small improvements to the script. It's possible to create an app with a single command now:

create_desktop_app_google_chrome.sh --name "Google Calendar" --url https://calendar.google.com --icon /tmp/calendar.png -q

There is also some sort of documentation about the command line arguments:

create_desktop_app_google_chrome.sh --help

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