[ Team LiB ] |
2.4 Script RunnerBy script runner I mean an application that accepts compiled script files and runs them. It typically has no facilities for editing or compiling scripts; you create and compile the scripts in a dedicated editor such as the Script Editor, and save the script as a compiled script file. Usually the file must then be placed in a particular location where the script runner program can find it. The script runner program typically provides some sort of interface for selecting a compiled script; when you select one, the script runner causes it to be executed. Since the script is compiled beforehand, a time-consuming step (compilation) is skipped, and execution thus typically proceeds considerably faster in a script runner than it does in an internally scriptable application where the code must be compiled on the fly. An extreme example of a script runner is the Script Menu provided by Apple. It's extreme because a script runner is all it is; it has no other purpose, and it has no other interface apart from the menu of scripts. If you don't see the Script Menu in your menu bar (it appears as a black scrolled s-shaped icon), Script Menu isn't running on your machine; you can start it up by running /Applications/AppleScript/Install Script Menu. The menu items in the Script Menu represent the folders and script files inside /Library/Scripts and ~/Library/Scripts; you can toggle the visibility of the menu items representing /Library/Scripts with the Show/Hide Library Scripts menu item. To add an item to the menu, put a compiled script file inside one of these directories. The menu is global, but if you create a folder ~/Library/Scripts/Applications/AppName (where for "AppName" you supply the name of some application), then scripts in it will appear in the Script Menu only when AppName is the frontmost application.[1] To run a script, choose the corresponding menu item from the menu. Many of the scripts in the Script Menu are worth studying as examples of scripting techniques. (Some of them are shell scripts or Perl scripts; in general these are outside the scope of this book, but it's nice that the Script Menu can run them as well.)
As an alternative to the Script Menu, you might want to use some sort of launcher. This term covers various kinds of utility that can be used to open things (folders, applications, files); such a utility may have other powers as well, but the important thing here is that it can run a compiled script file. A launcher will give you an interface, such as a keyboard shortcut or a "dock" with clickable icons, that you can associate with an action; these would then be ways to run a script. Furthermore, launchers often make it possible for you to specify that a particular keyboard shortcut or dock should be active only in a particular application. Some examples of launchers are iKey (http://www.scriptsoftware.com/ikey/), Keyboard Maestro (http://www.keyboardmaestro.com), and DragThing (http://www.dragthing.com). There are also various applications that are script runners in a secondary sense; they are intended for something else entirely, but they also act as script runners. Typically the point is to give you a convenient way to customize the application itself. The Script Editor is an example. Recall that the Script Editor provides a contextual menu that lists the contents of the /Library/Scripts/Script Editor Scripts folder. These contents are compiled scripts, and choosing an item from the contextual menu executes the corresponding script. Script Editor is thus a script runner. Of course the compiled scripts that appear in the Script Editor's contextual menu can do anything you like, but the primary purpose of this menu is as a repository for scripts that target Script Editor itself, like the ones that are already there. Another example is BBEdit, which has a Scripts menu in the menu bar. Again, whatever compiled scripts you place in a particular location (in this case it's the Scripts folder in the BBEdit Support folder) will appear as menu items in this menu and can be run by choosing the corresponding menu item. Furthermore, you can assign keyboard shortcuts to these menu items, which makes it even more convenient to access these scripts (as in a launcher). BBEdit is quite heavily scriptable, so this is a very convenient way to store and run scripted actions that customize BBEdit. (This convention, where a scriptable application has a Scripts menu so you can easily access scripts that drive that application, is an excellent one, and in my view is not sufficiently widespread. Some other applications that work this way are Script Debugger, Smile, Microsoft Entourage, Mailsmith, Tex-Edit Plus, and various Adobe applications.) BBEdit can also run scripts in other ways. Two more folders, Startup Items and Shutdown Items, are repositories for scripts that BBEdit will run automatically when you launch it and when you quit it. And scripts that you put in the Menu Scripts folder will be run before and after you choose from any of BBEdit's built-in menus. Thus BBEdit, by functioning as a script runner, lays itself open to a considerable amount of automation and customization by the user. It isn't uncommon, when applications work this way, for the developers to "seed" the Scripts menu by including some sample scripts in it by default. These can be worth studying as examples of how to drive the application with AppleScript. Some applications go even further, and actually incorporate scripts as normal menu items—that is to say, the application is scriptable and uses its own scriptability to implement some of the functionality present by default in the menu bar. Mail's Import Mailboxes menu item is said to work this way, and in Canto Cumulus more than a dozen standard menu commands are actually scripts. |
[ Team LiB ] |