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!
-Justin.

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

The Menulet Makes It Easy To See And Use Your AppleScripts

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!

Automator/Services

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

The Action is The Key... (Click To Enlarge)

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.

You Can Also Paste Your Script Into Automator (Click to Enlarge)

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

Name The Service (Click To Enlarge)

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

Try To Find A Memorable Shortcut (Click To Enlarge)

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!

Wrap Up

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!

13 Responses to “The Basics: Using Keyboard Shortcuts with AppleScripts”

  1. up0 September 28, 2010 at 11:34 pm #

    Great article. I’ve always wanted to use keyboard shortcuts especially for my mac based htpc using keyboard as remote for certain automation process.

  2. Rounik October 2, 2010 at 5:32 pm #

    Hey Justin,

    Thanks for linking to and including my article 🙂
    Just been browsing some of your more recent posts. Excellent! I love your style.

    Best
    Rounik

    • Justin Lancy October 2, 2010 at 8:02 pm #

      Very kind — thank you! 😀

  3. PseudoDiagonal August 17, 2011 at 1:05 pm #

    Hey Justin,

    Thank you for this article. I have no programming background, but found this article and within 5 minutes had my shortcut made to my applescript service!

    Cheers,

    Pseudo

    • Justin Lancy August 17, 2011 at 6:21 pm #

      Pseudo — Compliments like yours keep me smiling… and writing! 😉

      Thank you!!!

  4. Joey October 30, 2012 at 7:30 pm #

    Thanks for this… I followed the steps you laid out, and I got my Service to work, but only when I’m in the Finder. If I’m in any other open application, then the Keyboard Shortcut to switch to a particular app (using ^7) doesn’t work.

    Not sure what I’m doing wrong.

    • August March 7, 2013 at 2:37 pm #

      @Joey — Could you maybe have the application set in the Service Workflow?

  5. Gary January 4, 2013 at 11:15 pm #

    Many thanks for your post. Your instructions on using Services to assign a key to an AppleScript allowed me to set up a key to eject an external DVD drive (I’m on a 2012 iMac, so no internal drive!).

  6. Rcpagan April 24, 2013 at 1:03 pm #

    Hi and thanks for the great job you do with this website. I am new to Mac but an advanced windows user. I am trying to automate the Apple Mail to Evernote script you also provided on this website, thanks for that too. The script works in finder (in the mail script folder) but when I add it to the automator it gives e an error. I assume that is because the “script” needs to be in the mail script folder and not just anywhere. How do i link a keyboard shortcut to the script in the mail script folder?
    I hope it makes any sense, again thanks for sharing your hard work.

    • Justin Lancy April 27, 2013 at 9:55 pm #

      I’m assuming you pasted the code from the Mail script into Automator? One of the… peculiar… things about AppleScript is that the same code which runs fine in the AppleScript Editor won’t work inside of Automator without some changes.

      I know. It’s crazy.

      The Mail Script is a pretty complicated AppleScript and, as such, can’t tell you off-hand which parts you’d need to edit. If you’re just looking for an easier way to run the script, you might give FastScripts a look. That app – or a more advanced “keyboard launcher app” like Alfred – might be the way to go in your particular situation.

      Good luck! 🙂

  7. a thankful reader April 27, 2013 at 7:46 pm #

    This is great! See the link to it on a MS Office Forum thread about being unable to edit the subject line in Outlook 365.

    Thank you so much!!

  8. Branden September 5, 2013 at 8:25 pm #

    Hi Justin,
    Thanks for this extremely helpful site, and explanations that make it easier to understand for a noob.

    I just installed the email to evernote script, (yay! thanks!) and it works when I run it as a service in apple mail, but when I use the shortcut it doesn’t open the script window *and* a blank note appears in my evernote. Should I look to FastScripts, or might there be a simple oversight fix? Thanks again!