Summary

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

TIP

The Script Menu can be used to access Perl and shell scripts in addition to AppleScripts. Any script files placed in Scripts folders will be added to the list.


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:

  • close - close sub-folders.scpt Closes any open subfolders when the folder with the attached script is closed.

  • add - new item alert.scpt Displays an alert when new items are added to the folder with the attached script.

  • open - show comments in dialog.scpt Shows any comments stored when the folder with the attached script is opened.

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.

Table 4.1. Command Documentation Table for osacompile

-l <language>

Override the language for any plain text files. Normally, plain text files are compiled as AppleScript.

-o <name>

Place the output in the filename. If -o is not specified, the resulting script is placed in the file a.scpt.

-t <type>

Set the output file type to type. type is a four-character code. If this option is omitted and the output file does not exist, the type is set to osas that is, a compiled script.

-c <creator>

Set the output file creator to creator. Creator is a four-character code. If this option is omitted and the output file does not exist, the creator is set to "ToyS" that is, Script Editor.

-x

Save the resulting script as execute only.

-s

Run as a stay-open applet (doesn't exit when finished).

-u

Display a startup screen when the script runs.


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.

Table 4.2. Command Documentation Table for osascript

-e <command>

Enter one line of a script. If -e is given, osascript will not look for a filename in the argument list. Multiple -e commands may be given to build up a multiline script. Because most scripts use characters that are special to many shell programs for example, AppleScript uses single and double quote marks, "(", ")", and "*") the command has to be correctly quoted and escaped to get it past the shell intact.

-l <language>

Override the language for any plain text files. Normally, plain text files are compiled as AppleScript.

-s <flags>

Modify the output style. The flags argument is a string consisting of any of the modifier characters e, h, o, and s. Multiple modifiers can be concatenated in the same string, and multiple -s options can be specified. The modifiers come in exclusive pairs; if conflicting modifiers are specified, the last one takes precedence. The meanings of the modifier characters are as follows:

h Print values in human-readable form (default)

s Print values in recompilable source form

osascript normally prints its results in human-readable form: Strings do not have quotes around them, characters are not escaped, braces for lists and records are omitted, and so on. This is generally more useful but can introduce ambiguities. For example, the lists '{"foo", "bar"}' and '{{"foo", {"bar"}}}' would both be displayed as 'foo, bar'. To see the results in an unambiguous form that could be recompiled into the same value, use the s modifier.

e Print script errors to stderr (default).

o Print script errors to stdout.


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.

AppleScript Studio

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:

  • AppleScript Language Guide http://developer.apple.com/documentation/AppleScript/Conceptual/AppleScriptLangGuide/index.html

  • AppleScript in Mac OS X http://www.apple.com/applescript/macosx/

  • The AppleScript Sourcebook http://www.AppleScriptSourcebook.com/

  • AppleScript in a Nutshell Bruce W. Perry, ISBN: 1565928415, O'Reilly

     < Day Day Up > 


    Mac OS X Tiger Unleashed
    Mac OS X Tiger Unleashed
    ISBN: 0672327465
    EAN: 2147483647
    Year: 2005
    Pages: 251

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