// The Basics: Using Keyboard Shortcuts with AppleScripts
If you’re interested in learning more about AppleScript, be sure to check out the AppleScript and Automator Resources Page. It’s filled with links to books, videos, tools, and websites that’ll help you get started!
A reader sent me a message because she was having a hard time setting up a keyboard shortcut to run an AppleScript. She wasn’t a new Mac user by any stretch — in fact, she’s a tech professional! So I went back and re-read my own previous descriptions of how to assign a keyboard shortcut to a script.
Looking at it with fresh eyes, I realized that I could do better… and that it was important to do so. Proper use of keyboard shortcuts can have a massively positive impact on making your computer a lot easier to use!
In the same way, AppleScripts are designed to speed up the way you use your Mac. When people understand how they can use both scripts and shortcuts together — invoking complex actions with the same ease as cutting-and-pasting inside a document — they tend to become really enthusiastic about using them. For a “utility script” that you only use occasionally, it’s no problem to manually run it. But for the scripts you want to use repeatedly throughout the day, keyboard shortcuts aren’t just desirable — they’re essential!
So let’s take another pass at this and walk through two ways you can set up a keyboard shortcut to run an AppleScript.
FastScripts by Red Sweater (a.k.a., “What I Use”)
FastScripts is an application by Red Sweater Software that lets you manage your AppleScripts — and also the keyboard shortcuts that run them — from a menulet (a.k.a., one of those little icons in the menu bar near the clock).
Just click on the FastScripts icon and you are able to quickly assign keyboard shortcuts to all of your AppleScripts and also browse through your script collection for any given application. In fact, many times people don’t realize that there are AppleScripts already installed on their Macs for many programs. FastScripts helps you see what you’ve got on your Mac, run or edit your scripts, and — for the ones you use regularly — assign a keyboard shortcut to run them even quicker.
Here’s something else FastScripts is great at — context-sensitive shortcuts. For example, I am putting all sorts of stuff into Evernote from any number of programs throughout the day, and I’ve got several different AppleScripts on my system to that move that data into Evernote pretty seamlessly. Thanks to FastScripts, my “Send to Evernote Shortcut” (for me, ⌘ E (as in Evernote)) can be the same in every application, even though they each run different scripts to send their data into Evernote!
If you’re running a Snow Leopard or Lion-based system, another option is to use the Mac OS’s Services to run AppleScripts from a keyboard shortcut.
Compared to using FastScripts, it’s definitely a more involved process to get that going. I was going to write a detailed walk-through about how to set it up when I realized that Veritrope.com reader and MacProVideo Guru Rounik Sethi had already done an excellent one!
Rather than “re-invent the wheel”, I have excerpted and adapted his walk-through below (thanks to the kind permission of Rounik and the people at MacProVideo). If you’re interested in reading his complete blog post on the subject, please check it out at the excellent MacProVideo blog.
In case you’ve never heard of it before, Automator is a program included with every Mac which is designed to automate repetitive tasks. It enables you to create and specify workflows out of pre-made “Actions”. No programing knowledge is required! On Lion or Snow Leopard systems, Automator can then turn your workflows into “Services” or, as Apple describes them, streamlined shortcuts which “let you use features of one application while working in another”. If you’re interested in learning more about Automator, you should check out Rounik’s Video Tutorial or, if you prefer reading up on the subject, any of the books by Ben Waldie will be a fantastic help to you.
Like AppleScript, Automator and Services are part of what make owning a Mac a different experience than owning any other kind of computer — and setting up your keyboard shortcuts this way can be a simple and fun starter project to learn more about each!
Step 1 – Open Automator
For our particular purposes, we’re going to create a Service which runs our AppleScript. The reason for doing so is that, on Snow Leopard systems, Apple has given us the ability to set up a keyboard shortcut that launches a service. If that service runs an AppleScript then, voila, we’ve created a keyboard shortcut which launches an AppleScript. Get it?
So then, our first step after launching Automator will be to choose “Service” from the template window.
Step 2 – Set the Input
As you might have gathered from Apple’s description of a Service, we need to specify what situations (and in which applications) it will be available. For example, we wouldn’t want a service that is designed to copy the text from an email address and create a new Address Book entry to appear when selecting a picture in any application outside of Apple Mail.
This is an important point to remember. However for our Service, we want to disable the Input so we can launch our chosen AppleScript regardless of which application we are running and no matter what item(s) are currently selected.
Thus, our second step will be to choose “No Input” from the “Services Receives” drop-down menu — leaving the next selection as “Any Application”.
Step 3 – Taking Action
In Automator, we can create a workflow by combining actions. An action is a self-contained single task. By building a combination of actions, complex workflows can be created. Sometimes, like in this situation, a workflow may be quite simple and consist of only one action…. launching our Script!
Now — some AppleScripts are saved as “Script Files” (with a .SCPT extension) and some are saved as Applications. If you’re using Mac OS’s Keyboard shortcuts to launch your AppleScripts, I think you’ll see that it’s much easier to do so when they’re saved as Application files.
If your script file is saved as an Application, begin to type in “Launch Application” into the Search box above the Actions Library area on the left of the Automator window. After typing a few letters, it will be the only item displayed in the Library. Drag that “Launch Application” action into the Workflow area.
Inside the Launch Application action box in the Workflow area, choose your AppleScript application. Note — if it’s not in your Applications folder, you’ll need to select “Other” and browse to its location.
If your AppleScript is saved in the traditional script format, you have a couple of options. Of course, you can save a copy as an application and then follow the instructions above to set up the Action.
You can also take a different approach. Select the AppleScript code from AppleScript Editor and copy it to your clipboard. In Automator, begin to type “Run AppleScript” into the search box — after a few keystrokes, it will be the only action displayed. If you drag that action into the workflow area, you’ll find a box where you can then paste the script source code. There is at least one benefit to doing it this way: Your code will be contained within the Automator action and therefore you have one less file to keep track of than in the “Launch Application” configuration.
Step 4 – Save as a Service
You can test the workflow by clicking “Run” in the Automator Toolbar. The AppleScript you selected in Step 3 should launch. Now we know that the workflow functions correctly, let’s save it as a Service.
Choose File > Save as…
Choose a name that is short and descriptive and click Save when you’re done. You can now quit Automator and your Service can now be accessed from any application’s menu in the OS X Menu Bar.
(Choose Finder > Services to see if it is available.)
Step 5 – Assign a Keyboard Shortcut To Your Service
Open System Preferences and choose Keyboard > Keyboard Shortcuts [Services]
In the right window scroll down until you find your newly created service. Now double-click to the right of the Service name. When the text field appears, press the keys which you would like to become your shortcut. Also, make sure that the check box next to your Service is ticked — this activates the Service. (You can always return to this window and turn off the check box to disable this Service.)
Congratulations, you’ve just programmed your AppleScript Shortcut!
There are, of course, other applications and methods for using keyboard shortcuts on a Mac — but I think this is a good place to start for many people.
And if you have tips of your own, please share them with us in the comments below!