Section 4.8. Running Scripts from Text


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

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?

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.

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, TextEditand most other programs written in the last few yearswork just fine with Services.

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

4.8.1. 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 resultin this case, the number of files on your desktopright over your selected text.

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--8. (If your keyboard has a number keypad, you can also press -*, 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.

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.


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

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

Remember that the Run as AppleScript commandunlike, say, the Get Result of AppleScript commanddoesn'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.

Power Users' Clinic
Using Visual Basic from AppleScript

If you come from Windowsor a previous version of Microsoft Officeyou might be familiar with Visual Basic, Microsoft's homegrown language for automating Word, PowerPoint, and other Office programs. Visual Basic, while not nearly as English-like as AppleScript, is still used by thousands of people around the world to write macroslittle Visual Basic scripts. (It's also used to write computer viruses, but that's a story for another sidebar.)

By using the do Visual Basic command, you can run a specific Visual Basic commandor series of themright from within an AppleScript, like this:

tell application "Microsoft Word"     (*Print the front document, but don't     show a dialog box:*)     do Visual Basic ¬         "ActiveDocument.PrintOut" end tell

If you have a prerecorded Visual Basic macro stored in one of your documents, you can also run the entire macro with AppleScript's run VB macro command. (Keep in mind, however, that these AppleScript commands only work inside a tell statement that's targeted at a Microsoft Office program; you can't run Visual Basic scripts targeted at TextEdit or the Finder, for example.)

For more information on these Visual Basic commands (do Visual Basic and run VB macro), check out their entries in Microsoft Word's AppleScript dictionary. Or, if you have some time to spare, read Paul Berkowitz's excellent article on the subject at http://word.mvps.org/MacWordNew/WordAppleScript.htm.




AppleScript. The Missing Manual
AppleScript: The Missing Manual
ISBN: 0596008503
EAN: 2147483647
Year: 2003
Pages: 150

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net