4.8. Running Scripts from TextSo 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).
Running one of these Services commands is a simple process:
Script Editor has three different Services to offer, as described in the following sections. 4.8.1. Get Result of AppleScriptIf 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 resultin this case, the number of files on your desktopright over your selected text.
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.
4.8.2. Make New AppleScriptThis 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. 4.8.3. Run as AppleScriptThis 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.
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.
|