Skip to content

Instantly share code, notes, and snippets.

@zmwangx
Created March 22, 2015 23:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zmwangx/fad97e085045a21ebc1d to your computer and use it in GitHub Desktop.
Save zmwangx/fad97e085045a21ebc1d to your computer and use it in GitHub Desktop.
A bash script to back up app icons of all OS X applications living in /Applications or /System/Library/CoreServices
#!/usr/bin/env bash
function app_version
{
# $1 is the path to the app
/usr/libexec/PlistBuddy -c "print CFBundleShortVersionString" "$1"/Contents/Info.plist 2>/dev/null || date +%Y%m%d
}
function app_icon_path
{
# $1 is the path to the app
filename=$(/usr/libexec/PlistBuddy -c "print CFBundleIconFile" "$1"/Contents/Info.plist 2>/dev/null)
[[ -n ${filename} ]] || return
filename=$(basename "${filename}" .icns)
echo "$1/Contents/Resources/${filename}.icns"
}
function process_app
{
# $1 is the path to the app
name=$(basename "$1" .app | tr -d ' ')
path=$(realpath -e "$1") || { echo "${RED}error: broken link '${path}'${RESET}" >&2; return 1; }
version=$(app_version "${path}")
icon_path=$(app_icon_path "${path}")
[[ -n ${icon_path} ]] || { echo "${YELLOW}warning: '$1' has no app icon${RESET}"; return 1; }
[[ -f ${icon_path} ]] || { echo "${RED}error: '${icon_path}' does not exist${RESET}" >&2; return 1; }
cp "${icon_path}" "${name}-${version}.icns"
echo "${name}-${version}.icns"
}
find /Applications -maxdepth 2 -name '*.app' | while read app; do process_app "${app}"; done
find /System/Library/CoreServices -maxdepth 1 -name '*.app' | while read app; do process_app "${app}"; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment