AppleScript automation is one of OS X's most useful features. It lets you automate common, repetitive tasks so you can spend your time on more important things. Some AppleScripts would be even more useful, and their usage more seamless, if you could launch them by just hitting a key combination. And you can. Here are three ways to assign a keyboard shortcut to an AppleScript.
As context for this article, I'll be using David Battino's useful (at least to aesthetically demanding music fans) iTunes Fadeout Script as an example: when iTunes is playing, the script smoothly fades the volume to zero before pausing the track; if iTunes is paused, it will smoothly fade in, starting where it left off. (I prefer the version of the script that does not require any user intervention. It's the bottommost source code in David's article.)
Launch the Script Editor from the /Applications/AppleScript folder, create a new document window, copy and paste the source code from David's blog post into this window, and save the file to the Desktop as "Pause iTunes."
Before we actually assign a keyboard shortcut to this script, let's examine the four key locations on your Mac in which an AppleScript file should be stored. When and where you can access a script is determined by where you store the respective file. The scripts in three of these four locations are listed in the OS X Script Menu. To display this menu on the righthand side of the menu bar, launch the AppleScript Utility from the /Applications/AppleScript folder, and check the "Show Script Menu in menu bar" option. Figure 1 shows what you will see in that menu:
Figure 1. OS X's Script Menu. Note the submenu that provides easy access to the three scripts folders.
And this is where the scripts listed in the Script Menu are taken from:
The scripts inside this folder are accessible from all user accounts on the machine and are always listed in the Scripts Menu if you check the "Show Library scripts" option in AppleScript Utility. On a newly installed Mac, the library scripts are comprised of a number of more or less useful example scripts provided by Apple.
<your user folder>/Users/Library/
The scripts in this folder are only accessible when you are logged in as the respective user, but they are always available in the scripts menu, regardless of which application is currently frontmost.
<your user folder>/Library/Scripts/Applications/<application name>
Scripts that you store in one of these application-specific folders are shown in the Script Menu only if the respective application is active, e.g., the "Send URL to Mail" script for Safari shown in Figure 1 is only listed if Safari is the frontmost program when opening the Script Menu.
In addition to the scripts listed in the OS X Script Menu, some applications sport their own scripts menu, which is indicated by a little "paper scroll" icon between Window and Help menus in the application's menu bar. The scripts for this menu are usually found in:
<your user folder>/Library/Application Support/<application name>/Scripts/
Most of these script menus have an "Open Scripts Folder" item, which will open that folder in the Finder, so you don't have to navigate to it manually.
For starters, we will use the script right from within iTunes: select "Open Scripts Folder" from iTunes's script menu (under the "paper scroll" icon between the Window and Help menus), and move the "Pause iTunes.scpt" file from the Desktop into that folder.
When you open iTunes's script menu again, the script will show up without requiring a relaunch of the application. Select the script a couple of times to see it in action.
Now, let's see how we can launch that script with a keyboard shortcut.
Our first option for assigning a keyboard shortcut to an AppleScript is built right into OS X; via the Keyboard Shortcuts preferences, you can assign a shortcut to any item in an application's menus, including the script menu.
To assign a keyboard shortcut to our script, open the System Preferences, click on "Keyboard & Mouse" and select the "Keyboard Shortcuts" tab. Click on the plus sign beneath the list view. In the dialog sheet that pops up, select iTunes from the Application menu, type "Pause iTunes" into the "Menu Title" field, tab to the Keyboard Shortcut field, and type in the shortcut you would like to use. As a suggestion, I prefer Command-Option-M. ("M" as in "Mute.") Click the Add button (see Figure 2).
Figure 2. Adding a keyboard shortcut to our AppleScript in the Keyboard Shortcuts system preferences.
Scroll down to the bottom of the shortcut list, click on the disclosure triangle next to "iTunes," and there's our newly added keyboard shortcut. To change the settings afterwards, click on the menu item name or shortcut of the entry, and just type in the new setting.
For any changes in the Keyboard Shortcuts preferences to take effect, the respective application has to be relaunched, so close and relaunch iTunes. If you haven't made any typing errors, our "Pause iTunes" script now has a keyboard shortcut next to it, allowing you to invoke that script by pressing the key combination (Figure 3).
On a side note: if you'd like to assign a keyboard shortcut to a menu item that has an ellipsis "…" in it, remember that that ellipsis is (almost) always a single character instead of three dots. On a US keyboard, type Option-";" (i.e., Option-semicolon) to generate that ellipsis character.
Figure 3. Our Pause iTunes script in iTunes's script menu, featuring a keyboard shortcut.
There is one drawback to this approach: since we have placed the script in iTunes's own scripts folder, we can only access it from iTunes's menu bar. Consequently, the keyboard shortcut will also work only if iTunes is the active application.
To make the script accessible even when iTunes is in the background, we need to move the script file to the User Scripts folder (see "Where to store AppleScript files on your Mac") so it shows up in the Script Menu. Let's do that now: choose "Open Scripts Folder" from iTunes's script menu, choose "Open Scripts Folder > Open User Scripts Folder" from the OS X Script Menu, and move the file "Pause iTunes.scpt" from the former to the latter.
Please don't head over to the Keyboard & Mouse preferences just yet, because there is a peculiarity about the OS X Script Menu: this menu is controlled by System Events, a faceless background application that does not show up in the Application menu in the Keyboard Shortcut preferences. Consequently, this means that it is not possible to assign keyboard shortcuts to any scripts listed in OS X's Script Menu. Then again, it is possible, if we replace the built-in Script Menu with Red Sweater Software's FastScripts.
FastScripts is a vastly improved version of the Script Menu. It uses the same locations to search for AppleScript files to display, but lets you assign keyboard shortcuts to these scripts (Figure 4).
Figure 4. FastScripts script menu, including some items with keyboard shortcuts.
Adding a keyboard shortcut to any of the scripts listed in FastScripts is superbly Mac-like: hold down the Command key and click on a script in the menu, and FastScripts will open its Script Shortcuts preferences—which shows a complete, hierarchical list of all script files found in the User and Library Scripts folders—selecting the proper entry. Type the desired key combination, and you're all set (see Figure 5).
Figure 5. FastScripts compiles a list of AppleScript files for painlessly adding a keyboard shortcut.
To change a shortcut, double-click on an entry in the Script Shortcuts list and type in a new key combination. If you would like to remove a keyboard shortcut altogether, double-click the entry and press the Delete key. Note, by the way, that—unlike the approach via OS X's Keyboard & Mouse preferences—there is no need to restart FastScripts: the keyboard shortcuts show up, and work, right away.
In addition to supporting keyboard shortcuts, FastScripts also lists recently launched scripts, shows script execution errors in a separate window (whereas, in Script Menu, buggy scripts may fail silently), and, best of all, FastScripts is an application that is itself scriptable. It has an extensive AppleScript Dictionary, so you can do nifty things like this:
tell application "FastScripts" to invoke (the script items whose name is "Pause iTunes")
If you want to use global keyboard shortcuts for launching your AppleScripts, FastScripts simply is the most Mac-like way to achieve this. The software is available from Red Sweater Software's website, costs $14.95, and comes with a 30-day trial period. If you can make do without the Recent Scripts menu and need no more than ten keyboard shortcuts, you can opt for the free FastScripts Lite.
While FastScripts is perfect for assigning keyboard shortcuts to AppleScripts, there are two useful automation-related things it cannot do, yet: it does not list Automator workflows and you cannot use it to launch an AppleScript droplet with the current Finder selection. To integrate these two techniques in your automation endeavors, you should consider using an advanced application launcher like Quicksilver.
UPDATE: Starting with release 2.2.3, FastScripts supports folder aliases, so that, by placing an alias to <your home folder>/Library/Workflows in the <your home folder>/Library/Scripts folder, your workflows also show up in FastScripts's menu. According to the developer, future versions of FastScripts may automatically list Automator workflows.
You can choose from a range of application launchers for Mac OS X, but Blacktree's Quicksilver— which is all the rage on productivity-oriented websites right now, and deservedly so — is in a class all by itself.
Quicksilver compiles a catalog of objects, including files from a set of configurable locations on your Mac's harddrive, bookmarks from Safari, albums/artists/songs in iTunes, etc. To select an object in Quicksilver's main window (Figure 6), you type in a few letters, and Quicksilver will display a list with matching objects, preselecting what it considers the best match. With time, Quicksilver learns which letter combinations you use for which objects, and you can also define letter combinations manually, e.g., "M" for Mail, "S" for Safari, "SE" for Script Editor, or "MDC" for the Mac DevCenter website.
After selecting an object, you select an action in the same way, some of which may involve a target that, again, is selected the same way—e.g., for moving files to a folder, or opening a file with an application.
Figure 6. Quicksilver's main window, ready to open the Pause iTunes script file with Script Debugger. Note how few key strokes -- PAUSE <tab> OW <tab> SD -- this requires, especially for selecting the action and target, and think about how much navigation in the Finder it would require to drag-n-drop the file onto the application's icon.
The real beauty, however, lies in Quicksilver's expandability. There are countless plugins available that expand the searchable objects and actions, resulting in a very seamless workflow encompassing just about anything you do with your data on your computer. OK, enough praise for Quicksilver already. Let's get back to our topic of keyboard shortcuts.
In order to have Quicksilver find our AppleScripts, we must make sure that they are included in Quicksilver's catalog of objects: open Quicksilver's Catalog preferences, select the Scripts section, and ensure that both "Scripts (All Users)" and "Scripts (User)" are checked. If not, check both and click on the "circular arrow" next to the "I" button at the bottom of the window to make Quicksilver rescan the respective folders so they are available to us right away (otherwise, they'd be available after Quicksilver's next regular rescan interval as defined by the "Rescan" pop-up menu). (See Figure 7.)
Figure 7. Make sure that the AppleScript folders are included in Quicksilver's object catalog.
Now, let's assign a keyboard shortcut to our "Pause iTunes" script, say Control-M.
As a piece of advice, creating global shortcuts that do not contain the Command key helps minimize the risk of a conflict with existing keyboard combinations in applications, because there are only very few applications, like Terminal or some text editors, that use keyboard shortcuts without the Command key.
In Quicksilver's Triggers preferences, select the Custom Triggers on the left. Now click the plus sign and choose HotKey from the menu. In the dialog sheet that pops up, start typing "Pause iTunes" in the upper field until the "Pause iTunes" script is selected. Usually, the action field should already show "Run." If it doesn't, tab into it and type "Run." Hit Return to add this task to the Triggers list. (See Figure 8.)
Figure 8. Defining the Trigger task for running the Pause iTunes script.
Double-click on the newly created trigger. This will bring up the options drawer, in which you can define the actual keyboard shortcut in the Hot Key field (Figure 9).
Figure 9. Defining the key to trigger our newly created task.
As it is now, hitting Control-M will launch our "Pause iTunes" AppleScript, regardless of which application is currently active. If you want, however, you can also limit the scope for this keyboard shortcut by clicking on the Scope tab in the options drawer, choosing the appropriate scope from the pop-up menu, and if needed, typing a list of applications in the text field (Figure 10).
Figure 10. Limiting the scope of keyboard triggers.
Limiting the scope of the keyboard shortcut is not the only advantage Quicksilver has over FastScripts. Thanks to a special kind of object called Proxies, Quicksilver can access the Current Application, Current iTunes Playlist, Current Selection, etc. (To enable Proxies, check the Enable Advanced Features option in Quicksilver's Preferences → Application preferences.)
The Current Selection can mean a lot of things, like the currently selected text in a text editor, the currently selected tracks in iTunes, or—and this is what we will need—the currently selected files in the Finder. By using the Current Selection proxy, we can tell an AppleScript droplet to launch with the files selected in the Finder, just as if we had used the mouse to drag and drop the files onto the droplet's icon.
Assume we have an AppleScript droplet called Create Image Icon that creates image icons for any image files you drop onto it. In Quicksilver, you can create a keyboard shortcut that will run this script on the current Finder selection by creating a new keyboard trigger for a command that has "Current Selection" as the object, "Open with…" as the action, and "Create Image Icon," the AppleScript, as the target.
Now, whenever you select some files in the Finder and hit the keyboard shortcut you have assigned to this command, the AppleScript will run on the Finder selection. No need to move your hand to the mouse to drag any files onto the droplet. Also, you can neatly store the droplet somewhere other than your desktop, because you don't have to have it within "drag-n-dropping distance."
Caveat: Unfortunately, as of this writing, this process does not work as it should: when you select more than one file in the Finder and launch the AppleScript droplet with this selection via Quicksilver, only the first file in the selection is handled by the AppleScript. It sure would be nice if this was fixed in an upcoming version of Quicksilver. Until then, you can at least use this technique for one file at a time.
Since, unlike OS X Script Menu and FastScripts, Quicksilver's search for script files is not limited to specific folders, we might as well assign keyboard shortcuts to Automator workflows, too. All we have to do for this is tell Quicksilver to include the Automator files in its Catalog. Here's how.
Open Quicksilver's Catalog preferences, click on the plus sign at the bottom and choose "File & Folder Scanner" from the pop-up menu. Navigate to:
<your home folder>/Library/Workflows
This is where Automator stores its workflows. After adding this folder to the list, Quicksilver will switch to the Custom section. Select the "Workflows" item and click on the "I" information icon to show its options. Select "Folder Contents" from the "Include Contents" menu and, since Automator may store some of your workflows inside application-specific subfolders, choose a (scanning) Depth of 3, so that Quicksilver will "dive" into the subfolders to find all the workflows. Once that's done, you can add a keyboard shortcut to Automator workflows just as you did for the AppleScript files. (See Figure 11.)
Figure 11. After adding the Automator folder to its Catalog of objects, Quicksilver lets you assign keyboard shortcuts to workflows, too.
If you haven't been using Quicksilver yet, you can download the software for free from the Blacktree website.
Quicksilver is a very powerful and flexible tool, and its depth and breadth can be overwhelming. To get started, therefore, you should check out the tutorials found in this list on the Blacktree website. My favorite entry-level tutorials on Quicksilver are those by Lifehacker's Adam Pash: Part 1, Part 2, Part 3.
There you have it: three options for assigning keyboard shortcuts to your AppleScript and even Automator files. Which of these is best for you?
With the built-in Keyboard Shortcuts system preferences, you can already assign useful shortcuts to application-specific AppleScripts. Although you have to be careful to not make typing mistakes while entering the menu item's name, and despite the lack of support for global shortcuts, this approach may be fully sufficient if you only need to "keyboardize" a few scripts. And it saves you the potential hassle of installing additional software on your Mac.
If you do wish to use global shortcuts, however, installing another piece of software is required. With FastScripts, you not only get support for global shortcuts. The software will also present you with the most user-friendly and most Mac-like way of managing keyboard shortcuts yet — as long as you're using plain AppleScript files in their standard folder locations.
Quicksilver does it all: launch Automator workflows and AppleScripts, send a file selection to be run on an AppleScript droplet, and even finely adjust the scope of any keyboard shortcuts you create. However, using Quicksilver comes at a price: the learning curve is steep and the application is also known for some instabilities. Then again, once you have grokked its usage philosophy and realize what you can generally do with it, Quicksilver may very well give you an overall productivity boost for your daily computing tasks that borders on spectacular.
Return to Mac DevCenter.