Skip to content

Instantly share code, notes, and snippets.

@scottrbaxter
Created May 4, 2018 14:10
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save scottrbaxter/8a150546cd4a306cbd8adcf3ce52fe8b to your computer and use it in GitHub Desktop.
Save scottrbaxter/8a150546cd4a306cbd8adcf3ce52fe8b to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -e
addEntries() {
# check if universal access / custom menu key exists
if defaults read com.apple.universalaccess com.apple.custommenu.apps > /dev/null 2>&1; then
defaults delete com.apple.universalaccess com.apple.custommenu.apps
fi
defaults write com.apple.universalaccess com.apple.custommenu.apps -array
# write all apps to custommenu
defaults write com.apple.universalaccess com.apple.custommenu.apps -array-add $(echo -e "$appList")
echo "All apps with custom shortcuts:"
defaults read com.apple.universalaccess com.apple.custommenu.apps
# Restart cfprefsd and Finder for changes to take effect.
# You may also have to restart any apps that were running
# when you changed their keyboard shortcuts. There is some
# amount of voodoo as to what you do or do not have to
# restart, and when.
killall cfprefsd
killall Finder
}
# We need the bundleid for each app
get_BundleId(){
mdls -raw -name kMDItemCFBundleIdentifier "$1"
}
createKeyboardShortcuts(){
# make life easier
app=""
appList=""
CMD="@"
CTRL="^"
OPT="~"
SHIFT="$"
UP='\U2191'
DOWN='\U2193'
LEFT='\U2190'
RIGHT='\U2192'
# TAB='\U21e5'
Global
defaults write NSGlobalDomain NSUserKeyEquivalents "{
'About This Mac' = '${CMD}${SHIFT}${OPT}A';
}"
# Finder
app=/System/Library/CoreServices/Finder.app
if [ -a "$app" ]; then
bundleid=$(get_BundleId "$app")
echo "Adding: $app $bundleid"
appList+="$bundleid\n"
defaults write "$bundleid" NSUserKeyEquivalents "{
'Show Package Contents' = '${CMD}${SHIFT}O';
'Show Next Tab' = '${CMD}${RIGHT}';
'Show Previous Tab' = '${CMD}${LEFT}';
'Screenshots' = '${CMD}${SHIFT}S';
'Downloads' = '${CMD}${SHIFT}D';
}"
defaults read "$bundleid" NSUserKeyEquivalents
echo
fi
# iTunes
app=/Applications/iTunes.app
if [ -a "$app" ]; then
echo "Adding: $app"
bundleid=$(get_BundleId "$app")
appList+="$bundleid\n"
defaults write "$bundleid" NSUserKeyEquivalents "{
'Equalizer' = '${CMD}E';
}"
defaults read "$bundleid" NSUserKeyEquivalents
echo
fi
# iTerm2
app=$HOME/Applications/iTerm.app
if [ -a "$app" ]; then
bundleid=$(get_BundleId "$app")
echo "Adding: $app"
appList+="$bundleid\n"
defaults write "$bundleid" NSUserKeyEquivalents "{
'Copy with Styles' = '${CMD}C';
'Find Cursor' = '${CMD}${OPT}/';
'Select Previous Tab' = '${CMD}${LEFT}';
'Select Next Tab' = '${CMD}${RIGHT}';
'Move Tab Left' = '${CMD}${SHIFT}${LEFT}';
'Move Tab Right' = '${CMD}${SHIFT}${RIGHT}';
'Look Up in Dash' = '${CMD}L';
}"
defaults read "$bundleid" NSUserKeyEquivalents
echo
fi
# Chrome
app=/Applications/Google\ Chrome.app
if [ -a "$app" ]; then
bundleid=$(get_BundleId "$app")
echo "Adding: $app"
appList+="$bundleid\n"
defaults write "$bundleid" NSUserKeyEquivalents "{
'About Google Chrome' = '${CMD}.';
'Extensions' = '${CMD}e';
'Select Next Tab' = '${CMD}${RIGHT}';
'Select Previous Tab' = '${CMD}${LEFT}';
'Task Manager' = '${CTRL}t';
'key-aws-accounts-menu' = '${CTRL}a';
'key-aws-regions-menu' = '${CTRL}r';
'key-aws-services-menu' = '${CTRL}s';
'key-confluence-systems' = '${CMD}${SHIFT}c';
'key-github-ansiblesite' = '${CMD}${SHIFT}a';
'key-jira-systems' = '${CMD}${SHIFT}j';
'key-namely' = '${CMD}${SHIFT}o';
'key-webstore-extensions' = '${CMD}${SHIFT}e';
'key-zendesk-systems-custom' = '${CMD}${SHIFT}z';
}"
defaults read "$bundleid" NSUserKeyEquivalents
echo
fi
# HipChat
app=$HOME/Applications/HipChat.app
if [ -a "$app" ]; then
bundleid=$(get_BundleId "$app")
echo "Adding: $app"
appList+="$bundleid\n"
defaults write "$bundleid" NSUserKeyEquivalents "{
'Quit HipChat' = '${OPT}${SHIFT}Q';
'Show Next Chat' = '${CMD}${DOWN}';
'Show Previous Chat' = '${CMD}${UP}';
}"
defaults read "$bundleid" NSUserKeyEquivalents
echo
fi
}
# Make it happen
createKeyboardShortcuts
addEntries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment