O'Reilly logo

AppleScript: The Missing Manual by Adam Goldstein

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Running Scripts from Text

So far in this chapter, you've created a series of scripts that make new text. In this section, the roles are reversed: you'll create new text and have it run scripts for you.

The key to this is the Services menu, which you can find inside any application menu (the bolded menu, directly to the right of the Apple menu in the menu bar). For example, if you're in TextEdit, you access the AppleScript-related commands by choosing TextEdit Services Script Editor (Figure 4-8).

Note

Services are a way of accessing one program's features while inside another program. For example, if someone sends you an email containing a short URL (one that doesn't have the http:// in front of it, like www.oreilly.com), you can select the URL with the mouse and then go to Mail Services Open URL.

After selecting Open URL from the Services menu, your Web browser jumps into action and opens that Web page for you. Pretty slick, huh?

The three AppleScript services. Unfortunately, only one has a keyboard combination, so you'll have to run the other two the old fashioned way: with the mouse.

Figure 4-8. The three AppleScript services. Unfortunately, only one has a keyboard combination, so you'll have to run the other two the old fashioned way: with the mouse.

Running one of these Services commands is a simple process:

  1. Type some AppleScript commands in the current program, then select them.

    If you're working in TextEdit, for example, you could type display dialog "Hello!" and then select it.

  2. Choose the AppleScript service you want to run from the menu bar.

Note

The Services menu doesn't work properly in certain programs that haven't been completely updated for Mac OS X. Microsoft Word, for example, won't let you use the Services menu. On the other hand, TextEdit—and most other programs written in the last few years—work just fine with Services.

Script Editor has three different Services to offer, as described in the following sections.

Get Result of AppleScript

If you select an AppleScript command (or series of commands) and choose "Get Result of AppleScript" from the Services Script Editor menu, your commands are run in the background and their result is pasted in place of your commands. Try it out by typing:

tell application "Finder"
    count every file in the desktop
end tell

Then select your text and choose Services Script Editor Get Result of AppleScript. Script Editor launches in the background, runs your commands, and pastes the result—in this case, the number of files on your desktop—right over your selected text.

Note

If you don't feel like mousing up to the Services menu, you can also use the convenient keyboard for this Service shortcut instead: Shift-

image with no caption

-8. (If your keyboard has a number keypad, you can also press

image with no caption

-*, using the asterisk from the number keypad.)

This command is more useful than it might seem. Since AppleScript can perform mathematical operations, you can use this feature as a quick text-based calculator, straight from TextEdit or Mail, for example. Figure 4-9 shows you how.

Left: Type out a mathematical expression, then select it. Right: After you choose Get Result of AppleScript, your text is replaced with the correct answer.

Figure 4-9. Left: Type out a mathematical expression, then select it. Right: After you choose Get Result of AppleScript, your text is replaced with the correct answer.

Make New AppleScript

This command takes any text you've selected in the current program and copies it into a new document in Script Editor. This feature is particularly handy if you find some AppleScript code posted on the Web; just select the code and choose Services Make New Script, and you won't have to copy and paste the code yourself.

This command has another use, although it's much less obvious. You can use the service while inside Script Editor to select a portion of an existing script, and then have that code quickly copied into a new script window. That way, if you have a big script, you can copy sections of it to make smaller scripts with more specialized purposes.

Run as AppleScript

This command works almost exactly the same way as "Get Result of AppleScript," with one key difference: it won't replace your commands with their result. That makes this command perfect for quickly running, say, a line or two of code that you entered in an already-open TextEdit document.

Of course, since you can access the Services menu from Script Editor, you can run this command from Script Editor, too. In fact, this command adds a handy feature to Script Editor: you can use it to test only a few lines of a script rather than running the entire script at once. Simply select the lines you want to run, choose "Run as AppleScript" from the Services Script Editor menu, and sit back as AppleScript runs only your selected lines.

Note

Remember that the Run as AppleScript command—unlike, say, the Get Result of AppleScript command—doesn't return the result of your AppleScript commands. To work around this issue, place display dialog commands in your script to show you the status of events in your AppleScript code as they happen.

For even more fun, try running these three services from within Stickies (in your Applications folder). If you prepare a few sticky notes with your favorite AppleScript code written on them, you can then run the scripts by selecting the Run as AppleScript command. In this way, you can use Stickies as an encyclopedia of your favorite AppleScript code.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required