Quickly create/edit keyboard shortcuts for menu items

keyboardmenu barscriptsoftware-recommendation

Currently, if I want to bind a new keyboard shortcut to a menu item, I need to go through a somewhat tedious, many-step process:

  1. Open System Preferences, then Keyboard pane, then App Shortcuts
  2. Click the + button
  3. Find the application in the menu
  4. Forget the exact name of the menu item I want to bind
  5. Go back to the other application, find the menu item, remember exactly what it’s called
  6. Go back to System Preferences and type that in the field
  7. Type the desired shortcut and click “Add”
  8. On testing it out, find that I somehow mistyped it and spend a couple minutes troubleshooting steps 5–7

For something so simple as binding a keyboard shortcut to a menu item, I feel like there should be a very easy way to achieve this in about two seconds.

Is there some sort of script or app which I can use to easily manage an app's keyboard shortcuts?

I would be equally happy with something resembling either of these:

  • A small script/background app which I can call, which prompts me to select a menu item and type my desired shortcut
  • A somewhat larger program which will list and let me edit every menu item and/or keybinding within a certain app

Best Answer

Here's the AppleScript for it:

on run

tell application "System Events"
    set theActiveApp to name of 1st process whose frontmost is true
end tell

set theApplicationID to id of application theActiveApp
set theMenuItem to text returned of (display dialog "Enter menu item name" default answer "Example: New Window")
set theShortcut to text returned of (display dialog "Enter shortcut (use these: ⌘=@,  ⌥=~,  ⇧=$,  ^=^)" default answer "Example: @$1")

do shell script "defaults write " & quoted form of theApplicationID & " NSUserKeyEquivalents -dict-add " & quoted form of (quote & theMenuItem & quote) & " " & quoted form of (quote & theShortcut & quote)

end run

So, it gets the current application's identifier and then adds your menu item name and shortcut as required. You can even click the application's menu bar without the popup going away!

However, note that this is merely a rudimentary version of what you wanted; it only adds entries. If you make a mistake, that's yours to manually fix (and it might not appear in System Preferences). Also, lastly, it did not seem to work in Automator or Script Editor, but worked fine for the Finder. As for the former two, it adds the entries but for some reason they don't show up in System Preferences and can't be used, even after restarting the app. I don't know why but hey, at least it's something ;)

Related Question