macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Hit-and-Run: Launching AppleScripts with Keyboard Shortcuts
Pages: 1, 2, 3, 4, 5

Application Scripts folder

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

Application-specific Scripts folders

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.

Getting Started

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.

On-Board Tools: The Keyboard & Mouse System Preferences

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
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
Figure 3. Our Pause iTunes script in iTunes's script menu, featuring a keyboard shortcut.

Pages: 1, 2, 3, 4, 5

Next Pagearrow