☁ Stormcloud

RSS

OS X: How to set up custom keyboard shortcuts that actually work

Since (Mac) OS X 10.4 “Tiger”, it’s been possible to define your own custom keyboard shortcuts for pretty much any menu command in any application (you can set these up in System Preferences → Keyboard → Keyboard Shortcuts). For the most part, it works pretty well, but one thing that has always annoyed me is that some shortcuts don’t work until the corresponding menu has been opened at least once since the application was launched. This bug has been around since 10.4; here we are, seven years later, and the same old problem still exists in OS X 10.8 “Mountain Lion”. I got tired of waiting for Apple to fix this, so here’s my general solution that should work for any app / menu command that stubbornly refuses to let you use your own shortcut until the menu is opened.

Get FastScripts and enable UI Scripting

The basic idea: a simple AppleScript triggers the desired menu command, and the script is assigned a keyboard shortcut via FastScripts. You can assign up to 10 shortcuts for free — though it’s well worth the registration cost if you use other kinds of scripts too (such as the excellent Doug’s AppleScripts for iTunes).

Got FastScripts installed? Cool. The next thing you need to do is enable UI scripting. Open System Preferences → Accessibility (called “Universal Access” if you’re using an older version of OS X), and make sure “Enable access for assistive devices” is checked.

Simple shortcuts

For each shortcut you want to set up, you just need a simple AppleScript. Let’s create one that triggers the File → Export… command in Preview (which unfortunately doesn’t have a built-in shortcut). You’ll need OS X 10.7 “Lion” or later for this.

First, open the Preview app. Now open the FastScripts menu (see below) → FastScripts → “Create Preview Scripts Folder” (or “Open Preview Scripts Folder”, if it already exists).

Now open AppleScript Editor. Copy the following code and paste it into a new script, then click “Compile” to make sure it was copied correctly.

tell application "System Events"
    tell application process "Preview"
        click menu item "Export…" of menu "File" of menu bar 1
    end tell
end tell

This seems like a good time to mention an important detail: for menu items with ellipses (…), you have to use an actual ellipsis (single character that contains all three dots). If you type three separate periods, it won’t match the menu command (unless the app developer also made the same mistake — which they sometimes do). On a US keyboard layout, press [option]+[;] (semicolon) to type a real ellipsis.

Save your script as “Export” in the folder that FastScripts created/opened in the Finder. Now go back into the FastScripts menu → FastScripts → Preferences, and click “Script Shortcuts”. Your new “Export” script should appear in the list under Preview. Double-click where it says “(none)” to assign a shortcut, then type whatever shortcut you want to use (I used [command]+[shift]+E).

Now open an image or PDF in Preview and give your new shortcut a try.

Shortcuts for menu items that change

If the menu command changes depending on the current state of the app, your script needs to check if the menu item exists and change the command accordingly. Here’s a script I created that toggles the folders sidebar in the new Notes app in Mountain Lion.

tell application "System Events"
    tell application process "Notes"
        if menu item "Show Folders List" of menu "View" of menu bar 1 exists then
            click menu item "Show Folders List" of menu "View" of menu bar 1
        else
            click menu item "Hide Folders List" of menu "View" of menu bar 1
        end if
    end tell
end tell

Questions/problems? Send me a message on Twitter.