Skip to content

Instantly share code, notes, and snippets.

@vitorgalvao
Created January 23, 2023 19:42
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 vitorgalvao/37edd4643414e30cea7ab6f6a28630de to your computer and use it in GitHub Desktop.
Save vitorgalvao/37edd4643414e30cea7ab6f6a28630de to your computer and use it in GitHub Desktop.
Rename Alfred Workflows in preferences directory to their name and bundle ID
#!/bin/zsh
readonly program="$(basename "${0}")"
function usage {
echo "
Rename Alfred Workflows in preferences directory to their name and bundle ID
Usage:
${program} run
The argument 'run' is necessary to prevent running the script by mistake.
Options:
-h, --help Show this help.
" | sed -E 's/^ {4}//'
}
function skip {
echo -e "$(tput setaf 3)${1} Skipping…$(tput sgr0)"
}
function failure {
echo -e "$(tput setaf 1)${1}$(tput sgr0)"
exit 1
}
if [[ "${1}" =~ ^(-h|--help)$ ]]; then
usage
exit 0
fi
if [[ "${1}" != 'run' ]]; then
usage
exit 1
fi
# Get Workflows directory
readonly alfred_prefs="$(/usr/bin/osascript -l JavaScript -e 'const prefs = $("~/Library/Application Support/Alfred/prefs.json").stringByExpandingTildeInPath.js; const data = $.NSFileManager.defaultManager.contentsAtPath(prefs); string = $.NSString.alloc.initWithDataEncoding(data, $.NSUTF8StringEncoding).js; JSON.parse(string)["current"]')"
readonly workflows_dir="${alfred_prefs}/workflows"
echo "Running in ${workflows_dir}…"
for dir in "${workflows_dir}"/*; do
[[ -f "${dir}" ]] && continue
info_plist="${dir}/info.plist"
if [[ ! -f "${info_plist}" ]]; then
skip "${dir} has no info.plist."
continue
fi
name="$(/usr/libexec/PlistBuddy -c 'print name' "${info_plist}")"
bundle_id="$(/usr/libexec/PlistBuddy -c 'print bundleid' "${info_plist}")"
[[ -n "${bundle_id}" ]] && id=".${bundle_id}" || id=''
new_dir="${workflows_dir}/$(tr -cd '[:alnum:]._-' <<< "${name}${id}")"
if [[ -d "${new_dir}" ]]; then
skip "Directory $(basename "${new_dir}") already exists at ${workflows_dir}."
continue
fi
echo "$(basename "${dir}") → $(basename "${new_dir}")"
mv "${dir}" "${new_dir}"
unset info_plist name bundle_id id new_dir
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment