|< Day Day Up >|
Additional AppleScript Tools and Resources
To finish this chapter, let's look at some additional tools on your system and other resources for AppleScript information. Apple has a strange habit of hiding AppleScript from its users. Although this has improved from Jaguar to Panther and Panther to Tiger, things that you'd expect to be plainly visible are tucked away.
Activating the Script Menu
The Script Menu is a menu extra to your menu bar that can be used to quickly launch AppleScripts from the /Library/Scripts folder or ~/Library/Scripts. Apple has included dozens of scripts you can use with your applications immediately. To turn this feature on, you must run the AppleScript Utility within the AppleScript folder and click the Show Script Menu check box.
Any compiled scripts placed in either of the Scripts locations will become accessible from the menu. To create submenus for categorizing scripts, just create multiple folders within the Scripts folders. As with everything in Mac OS X, items stored in /Library/Scripts are accessible by all users, whereas those in your personal ~/Library/Scripts folders can be used by only you. If you prefer to see only your own scripts, you can choose to not show library scripts from within the AppleScript Utility.
To remove the Script Menu, Command-drag it from the menu bar or turn it off within the AppleScript Utility.
Using Folder Actions
Folder Actions are scripts executed when folders are opened, modified, or moved. Actions are configured either via the Script Menu's Folder Actions submenu or by selecting a folder in Finder and invoking the contextual menu (Control-Click).
First, Folder Actions must be enabled. Choose Enable Folder Actions from a folder's contextual menu or the Script Menu.
Next, you can attach a Folder Action to a selected folder using either the Attach Folder Action or Configure Folder Actions option from the same menu.
The Add Folder action prompts you for a Folder Action script to attach to the highlighted folder, whereas Configure Folder Actions opens the Folder Actions Setup application (path: /Applications/AppleScript/Folder Actions Setup), shown in Figure 4.23, which provides access to all Folder Actions configured for your account.
Figure 4.23. Configure Folder Actions provides a control center for adding and removing Folder Actions.
Within the Folder Actions Setup window, use the Enable Folder Actions check box to globally enable or disable actions. To add a new action, click the + button below the left column and choose a folder you want to attach an action to. When added to the folder list, highlight it and use the + button in the right column to choose a Folder Action script that you want to attach to the folder. The - button can be used to remove folders and attached scripts, whereas the Open Folder and Edit Script buttons open the highlighted folder and open the selected script in Script Editor, respectively.
To get started with Folder Action scripts, Apple has included three basic scripts in /Library/Scripts/Folder Action Scripts:
Also included are several scripts for operating on images, providing simple graphic conversions and alterations just by placing files in a folder.
Properly formed Folder Action scripts should be placed in either /Library/Scripts/Folder Action Scripts or ~/Library/Scripts/Folder Action Scripts. Apple has provided an excellent tutorial on how to set up a Folder Action script at http://www.apple.com/applescript/folder_actions/.
Installing Scripting Additions
Enterprising developers who open the power of their software to the AppleScript model constantly expand AppleScript. The most common type of scripting addition is a new application. Applications that you install might or might not be scriptable be sure to check the documentation or try opening the software's dictionary using the Script Editor.
In addition, some developers might deliver extensions to AppleScript in the form of a scripting extension. These extensions are not applications themselves but libraries of additional functions that can be used in any AppleScript.
Downloaded AppleScript extensions should be stored in ~/Library/ScriptingAdditions or the system-level directory /Library/ScriptingAdditions for access by all users.
Running Command-Line AppleScript Tools
AppleScript compilation and execution has been extended to the BSD shell through the use of the osacompile and osascript commands.
The osacompile utility accepts a text file containing AppleScript as input and outputs a compiled script file using the following syntax:
osacompile -o <output file> <script file>
Although this is probably the form you'll and use most, several additional command-line options can fine-tune the compile process. Table 4.1 documents several of these options.
If no options are specified, osacompile produces a classic Mac OS format script file that is, type osas (compiled script), creator "ToyS" (Script Editor), with the script data in the scpt:128 resource and nothing in the data fork. This format is compatible with all Mac OS and Mac OS X systems.
After you've compiled a script, you can run and it from the command line using the osascript utility.
osascript <script filename>
If a filename is not specified on the command line, osascript attempts to run AppleScript from standard input. This is a great way to test scripts or run a quick AppleScript command without needing to start the Script Editor. Like the osacompile command, osascript provides a number of command-line options that advanced users might be interested in. These are displayed in Table 4.2.
osascript normally prints script errors to stderr, so downstream clients see only valid results. When you're running automated tests, however, using the o modifier lets you distinguish script errors, which you care about matching, from other diagnostic output, which you don't.
After you've familiarized yourself with basic AppleScript syntax, you might want to consider moving up to the next level of AppleScript development: AppleScript Studio. AppleScript Studio is Apple's integration of the AppleScript programming language with XCode.
Using XCode you can quickly create complete GUI applications powered entirely by AppleScript. Although not appropriate for real-time or graphically intense software, AppleScript Studio can quickly create a GUI around Unix-based commands. In fact, a number of utilities (such as Carbon Copy Cloner, discussed in Chapter 29, "Maintaining a Healthy System") have been written in AppleScript Studio and have received rave reviews.
To get started with AppleScript Studio, install XCode and browse the examples in /Developer/Examples/AppleScript Studio. Apple provides a simple tutorial along with PDF reference guides to get you started. Be warned: AppleScript Studio takes advantage of Apple's development tools. These tools, although powerful, have been known to take some time to master.
Other Sources of AppleScript Information
AppleScript is a capable language that offers many advanced features impossible to cover in the amount of space this title allows. What is provided here should be an ample start to creating scripts of your own and editing scripts included with Tiger. If you're interested in more information on advanced AppleScript syntax, I strongly suggest that you check out the following resources:
|< Day Day Up >|