Appendix A. Standard Scripting AdditionsScripting additions are a powerful element of AppleScripting that give it almost infinite extensibility. These code libraries live inside the startup disk:System Folder:Scripting Additions folder in OS 9. Figure A-1 shows what their icons look like. Known among the scripting cognoscenti as osax (singular form, standing for Open Scripting Architecture Extension) or osaxen (a plural form), the scripting additions give you commands you can use almost anywhere in your script. Ever since Mac OS 8.5, Apple Computer has bundled a number of the most useful scripting additions into the Standard Additions file and installed this file with your operating system. In Mac OS X, the filepath for the Standard Additions file is /System/Library/ScriptingAdditions/StandardAdditions.osax. Figure A-1. Scripting addition files in OS 9The Standard Additions include the following scripting additions:
Figure A-2 shows the Standard Addition's dictionary in Mac OS 9. Figure A-2. Standard Addition's dictionary windowAny programmer, not just Apple's, can create a scripting addition. This mechanism has spawned numerous third-party osaxen (i.e., those not developed by Apple), which allow you to parse HTML or XML in scripts, use regular expressions in searches, negotiate a directory tree and do something with each encountered file (the walk folders command of Jon's Commands), and initiate many other tasks that you would otherwise have to program with your own code in AppleScript or not be able to accomplish with a script at all. Examples of some of these third-party scripting addition files are Akua Sweets, Jon's Commands, and XML Tools. The site http://osaxen.comcontains an osax database. How do scripting additions work in Mac OS 9? When you use a command in a script, the application that ends up receiving the command depends on the command's script context. The command or Apple event could be sent to one of the following targets:
When AppleScript searches for the target of a script command it looks, among other places, inside the Scripting Additions folder for an application, an application alias, or a scripting addition file. For example, I have used the display dialog scripting addition in code samples throughout this book. This displays a modal dialog to the user and optionally allows you to request them to enter some information into a text field before they dismiss the dialog box by clicking a button (or before it closes if you specify that the window disappears after a certain number of seconds). The reason you can just randomly include display dialog in your script (with some exceptions explained later) is that AppleScript will search the Scripting Additions folder for the recipient or handler of this command and find it within the Standard Additions file in Mac OS 9. Some applications, such as ColorSync Extension, do not allow the use of display dialog within the "tell" blocks that target them (e.g., tell app "Colorsync Extension"...). You will receive a "no user interaction allowed" error message. Example A-1 shows how two handy scripting additions, display dialog and offset, can be used in a script that involves different types of commands. Read the script comments to find out the targets of each command. Example A-1. Using Standard Additions in a Script(* AppleScript finds the display dialog osax inside the Standard Additions file, so you don't have to use any tell statements *) display dialog "Enter your first and last names" default answer "" set names to the text returned of the result (* names is set to a string like "Bruce Perry" or whatever the user enters. *) (* AppleScript also finds the offset command inside Standard Additions. Offset searches for one string inside of another and returns the character position as an integer or zero if it doesn't find the string(in this case the searched for character is a space character). We are using it to locate the space character that separates the first and last names. *) set sp to (offset of " " in names) (* The first name is pulled out of the string by getting all the characters up to but not including the space character that follows the first name *) if sp 0 then (* if a space character is found, there must be at least two names in the string *) set first_name to characters 1 thru (sp - 1) of names tell application "BBEdit 5.1" activate (* 'insert text' is a BBEdit command, so it receives the 'insert text' ¬ *) insert text names (* the display dialog command will not be sent to BBEdit, even though it¬ *) display dialog (first_name as text) end tell end if As an AppleScripter, you will become very fond of some osaxen and use them all the time. You will also discover new ones and realize "hey, this makes scripting web page downloads (or whatever) much easier!" This chapter describes the commands and classes included with the Standard Additions collection found inside the startup disk:System Folder:Scripting Additions directory in OS 9. Again, the Mac OS X path for this file is /System/Library/ScriptingAdditions/StandardAdditions.osax. This group of scripting additions is installed with Mac OS 9 and Mac OS X. The classes described in this chapter are objects returned by certain commands, such as the file information object (a record type or associative array in AppleScript) returned by the info for command. |