2

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
  • Some apps do that internally - usually the ones with half a million options, Photoshop & Cubase are two that spring immediately to mind. For the rest, there's your example method. – Tetsujin Apr 16 '17 at 19:28
  • I'd imagine this would be not terribly hard to program — These settings seem to be stored in NSUserKeyEquivalents keys in each application's preference plists (or the Apple Global Domain one, for globally-set shortcuts). You can easily see all the existing bindings with defaults find NSUserKeyEquivalents, and writing them isn't much different. – Timothy Mueller-Harder Apr 16 '17 at 19:55
  • Just tried that command - it doesn't even cover my keyboard sys prefs changes, let along the myriad I have in pshop & cubase. – Tetsujin Apr 16 '17 at 19:58
  • @Tetsujin Well of course it doesn't cover the changes done in proprietary ways! I'm only talking about the native functionality for this; those are kind of irrelevant. Though it's strange that it doesn't include all your bindings in System Preferences... it works fine for me. Are you running it with appropriate permissions? – Timothy Mueller-Harder Apr 16 '17 at 20:00
  • I had a list of precisely 4 items using that command [all 'true']. My actual prefs list is in the dozens, excluding those particular apps with built-in functionality, which show only in those apps. – Tetsujin Apr 16 '17 at 20:04
2

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 ;)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .