April 19, 2015

How To Assign Keyboard Shortcuts To AppleScript and Automator Actions

Last week, I posted a piece about How To Automate “Get/Send Clipboard” in Mac OS X Screen Sharing. In that article, I advocated using the Mac’s Speech Recognition to launch the AppleScript commands from any application (except Screen Sharing itself). But what if you don’t want to talk to your computer? Here’s a method of launching AppleScript or any other Automator action using keyboard shortcuts regardless of what application you are currently in. And it uses no third-party software, a bit plus as far as I’m concerned!

Keyboard Shortcut Limitations

One of the best aspects of Mac OS X is that many useful features are centralized and uniform across all applications. This includes keyboard shortcuts, which can be created modified for any application using the System Preferences panel. But there’s a frustrating limit to Mac Keyboard Shortcuts: You can only assign shortcuts to menu bar commands, not any other application actions. Therefore, it’s not obvious how to assign keyboard shortcuts to AppleScripts, application launching, and other non-menu activities.

This is pretty much the exact opposite of the limits of AppleScript programming. AppleScript and Automator make it easy to automate actions within applications but difficult to access the menu bar! But there is a loophole to both limits.

Automator Services

Use Automator to add your AppleScript as a universal Service

Recent versions of Automator circuitously address this limitation by allowing you to create Services. Since these appear in the menu bar (in the Application’s main menu), they can have keyboard shortcuts assigned to them. It just take a bit of cleverness to make them work correctly!

Since Automator can run arbitrary AppleScript commands as well, it’s possible to redo our Screen Sharing script as an Automator service and make it work across all Mac OS applications!

First, open Automator and create a new service. Choose the “Service” template, select “Utilities” from the Library, and double-click “Run AppleScript”. Replace the “(* Your script goes here *)” portion with your desired AppleScript commands.

Shortcuts Everywhere!

You can assign a keyboard shortcut to the menu item for a service created in Automator

Now comes the trick. Services are context-sensitive (cool!) but we’re creating one we want to always be available. Notice the menus for “Service receives selected text in any application” at the top of the Automator screen? This is where you set the context for that service. Since we want our “get” and “send” commands to be available regardless of the context, we’ll change the first box to “no input”. This tells Automator to always include this service in the menu.

Save the resulting service with a descriptive and unique name and we can move on to add the keyboard shortcut. Remember that name! Maybe even copy it to the clipboard!

Open the Keyboard Shortcuts pane in the Keyboard panel in System Preferences. Click the plus sign to add a new shortcut and enter the name of the service exactly as you typed above. Assign a key to activate it and you’re ready to go.

Now whenever you press that key, in any application or context, it will run your AppleScript command. You can use this to automate just about anything, from launching an application to locking the computer to initiating a background process.

If only Apple didn’t make it so obtuse!