2.3. Script RunnerAn application without facilities for editing or compiling scripts may nevertheless offer to execute compiled scripts for you on demand by way of some convenient interface, such as a menu . Such an application might be called a script runner . There is usually a requirement that a compiled script file be placed beforehand in some particular location where the script runner can find it. Because the script is compiled beforehand, a time-consuming step (compilation) is skipped, and execution typically proceeds considerably faster in a script runner than it does in an internally scriptable application where the code must be compiled from text on the fly. Many scriptable applications act as script runners, typically by means of a special Script menu . This behavior is helpful because, having developed a script that drives such an application in a useful way, you might like some convenient interface for executing that script on future occasions; the application's Script menu provides such an interface. Scripts in an application's Script menu do not have to target that application, but the feature makes sense, and is provided, in the expectation that they will do so. (And when a script in an application's Script menu does target that application, there is sometimes a tremendous speed advantage over running that same script from elsewhere; see Chapter 22.) For instance, as mentioned under "Customization" in Chapter 1, if you put scripts into ~/Library/iTunes/Scripts/, iTunes will generate a Script menu listing them and permitting you to run them; so that's a good place to store and access your scripts that customize iTunes. BBEdit is a particularly fine example of a script runner. Whatever compiled scripts you place in ~/Library/Application Support/BBEdit/Scripts/ will appear as menu items in BBEdit's Script menu, where a script can be run by choosing its menu item. For even more convenient access, BBEdit lets you assign keyboard shortcuts to these menu items. BBEdit also implements some further conventions that have become a sort of de facto standard: you can edit a script by holding Option as you choose it from the Script menu, and you can reveal a script file in the Finder by holding Shift as you choose it from the Script menu. BBEdit also provides some useful naming conventions for setting the order of the items in the Script menu (otherwise the scripts would always appear in alphabetical order, because that's how the Finder supplies them); read the BBEdit manual to learn more. As an alternative interface, BBEdit also lists and lets you access scripts in a floating window. Some other programs that act as script runners through a Script menu are Script Debugger, Smile, Microsoft Entourage, Tex-Edit Plus, and various Adobe applications. Apple's Script Editor takes a different approach: instead of a Script menu in the menu bar, it lets you access the compiled script files in a specific location by way of the contextual menu (see "Apple's Script Editor," earlier in this chapter). An application can act as a script runner in a noncustomizable way as well. For example, an application might incorporate scripts as ordinary menu items. This makes sense when an application is scriptable and can best implement a command's functionality by taking advantage of its own scriptability. Mail's File Import Mailboxes menu item works this way. There is also a Script Menu provided by Apple that gives systemwide access to scripts; this is particularly helpful when a script needs to be available from every application, or when its target application has no Script menu of its own. It appears as a status menu item (on the right side of the menu bar) in the form of a black scrolled s-shaped icon. If you don't see it, you can turn it on with the AppleScript Utility. The menu items in the Script Menu represent the folders and script files inside /Library/Scripts and ~/Library/Scripts; AppleScript Utility provides an option for toggling the visibility of the /Library/Scripts items. The menu is global, but there is a special convention for specifying that a script should appear only when a particular application is frontmost: create a folder called ~/Library/Scripts/Applications/<AppName> (where for "AppName" you supply the name of some application), and put the script inside that. The Script Menu can also run Unix shell scripts, including Perl scripts. The Script Menu follows the BBEdit conventions for the Option and Shift keys. A utility similar to the Script Menu, with some advantages such as keyboard menu shortcuts and easier creation of application-specific folders, is Red Sweater's FastScripts . An alternative of a completely different kind, using contextual menus instead of a drop-down menu in the menu bar, is Ranchero's Big Cat . And Xendai's Bellhop lets you run a script from the Services menu that appears in most applications. Still another script runner interface is provided by a launcher application. A launcher is a utility used to open things (folders, applications, files); many launchers will "open" a compiled script file by running it. My favorite is DragThing . DragThing's primary interface is a "dock" with clickable icons, where each icon is a file or folder; launching a compiled script with DragThing either runs it or opens it for editing, depending on whether you're holding down the Option key. An icon can also be assigned a keyboard shortcut that launches it, and docks can be set to be active only in a particular application or set of applications. Other examples of launchers are iKey and Keyboard Maestro . |