APPLESCRIPT TOOLS, SCRIPTING ADDITIONS, AND SCRIPTABLE APPLICATIONS
Mac OS X 10.2 includes a few system components that have new AppleScript commands and other items in their AppleScript dictionaries, as well as a few altogether new items that are scriptable, such as the Digital Hub Scripting and Keychain Scripting scripting additions. This is, as always, good news for AppleScripters.
If there is a theme to the AppleScript changes in Mac OS X 10.2, it is that a number of scriptable system components have been created as Cocoa applications or turned into Cocoa applications. In the process, all of their dictionaries have gained the usual Cocoa Standard Suite and Text Suite of AppleScript commands. Some existing components have lost part of their former AppleScriptability, but this is probably due to time constraints and therefore temporary. Examples are Apple System Profiler and Print Center, discussed below. A counterexample is Help Viewer, also discussed below, whose scriptability has been enhanced.
I am a champion of Cocoa, and I am happy to see more of the system tools becoming Cocoa applications. Among other things, this means that as Cocoa’s built-in scripting dictionary improves in the future, these applications like all scriptable Cocoa applications will automatically gain the benefit of many of the changes. Apple continues to improve the Cocoa AppleScript dictionary, although some improvements (including improvements made in Jaguar) require incorporation of new features by developers and therefore won’t show up immediately in applications.
Installed Software and Dictionary Changes
Address Book 3.0.1 (v211)
Address Book, new in the /Applications folder in Mac OS X 10.2, is a Cocoa application. Its dictionary includes not only the usual Cocoa Standard Suite and Text Suite, but also an Address Book Script Suite containing many classes and three commands: Add, Remove, and Save Addressbook. Look under the Mail application, below, for information about scripts that come with Mac OS X for importing addresses from other popular Personal Information Managers.
I have done only limited experimentation with scripting Address Book. The promise is high, but preliminary reports from the field indicate that there may be some potentially serious problems with Address Book scriptability at this time. For example, indications are that adding persons via AppleScript can render the database unusable by other applications that come with Mac OS X 10.2, and the publisher of FAXstf reports that fax numbers saved using AppleScript are not recognized as fax numbers due to an issue in Address Book.
Apple’s AppleScript Web site indicates that an article is forthcoming about how to script Address Book, but the link isn’t yet live at this writing.
Apple System Profiler 3.0
Apple System Profiler, in /Applications/Utilities, has turned into a Cocoa application, and in the process it has lost virtually all of its useful AppleScriptability. In the previous version, you could use AppleScript to access individual properties of your computer, such as Rated Speed, Model Name, and many others. You could also get the entire report as tab delimited text. In the new version, the usual Cocoa Standard Suite and Text Suite are included, but the only profile information you can obtain via AppleScript is the entire report as Unicode text. This report isn’t tab delimted. Worse, it is formatted for human readability with ASCII-style dashed lines for section headers, so AppleScript can’t parse it in any reasonable way.
Rumors and other indications prior to the release of Jaguar suggested that Apple System Profiler’s scriptability would be enhanced in Jaguar. Perhaps the deadline overtook this effort, and improvements to the application’s scriptability will hopefully become available in a future release.
Digital Hub Scripting
Digital Hub Scripting is a new scripting addition in /System/Library/ScriptingAdditions. It implements five actions. You can write a script containing handlers with these names, just as you do with Folder Actions, and the handlers will be triggered when the corresponding user action takes place, such as inserting a music CD or a video DVD. The other three actions recognize insertion of a picture CD, a blank CD, and a blank DVD.
DVD Player 3.2
DVD Player is in the /Applications folder. Version 3.1 introduced scriptability in March 2002, and version 3.2 appears in Mac OS X 10.2. It includes an Application class that gives you control over the size and position of the DVD controller, info, and video windows and access to many DVD features such as the current state of the DVD player (e.g., Playing, Paused), whether a DVD is inserted, the Elapsed Time and Remaining Time, and Audio Track and Chapter information, to mention only a few. It also includes several commands, including Step DVD, Stop DVD, Pause DVD, and Play DVD. This extensive scriptability is made all the more useful by the new Digital Hub Scripting scripting addition, which enables you to begin running a script containing DVD Player commands the moment a DVD is inserted.
Finder
The Finder’s Legacy suite now includes the Desktop Picture property of the Application class, moved there from the full Application class. Finder is in /System/Library/CoreServices/.
Help Viewer 2.0.1
Help Viewer is found in /System/Library/CoreServices/. It is now a Cocoa application in Mac OS X 10.2, and it has the entire Standard Suite and Test Suite that come automatically with scriptable Cocoa applications. It also continues to have a small Help Viewer suite designed to be of use to authors of Apple Help modules. In Mac OS X 10.2 this includes new commands to Go Back, Go Forward, and Go Home, emulating the functions of the buttons in the GUI, and new Handle URL and Lookup Anchor commands.
iCal 1.0
iCal 1.0 was released in September 2002, after the release of Mac OS X 10.2, but it was an integral part of the Jaguar promotion and we consider it part of Jaguar. This calendar and to-do application in the /Applications folder is scriptable. As a Cocoa application, it has the usual Cocoa Standard and Text Suites. In addition, it has Calendar, Application, Todo, and Event classes to make to-do’s and events scriptable. It also has a GetURL command, since iCal can share calendars remotely. Oddly, it includes a Create Calendar command, although it also includes the Make command from the Standard Suite and a Calendar class which should have made a hybrid Create Calendar command unnecessary.
iDVD 2.1
Apple has made much of iDVD’s scriptability. The iDVD updater was released shortly before the release of Mac OS X 10.2, and I believe it is part of Jaguar, as well. The Web site includes many useful iDVD scripts and the iDVD Companion, an AppleScript Studio application.
The iDVD Web site includes instructions for fixing iDVD 2.1 if it quits unexpectedly when you try to open its dictionary (as it did on my computer). The problem is a missing dictionary resource, which you can download and install yourself to fix the problem.
This is a Cocoa application in the /Applications folder; as such, it includes the usual Cocoa Standard and Text Suites. It also includes an iDVD suite with nine classes and five special commands to help you automate the creation of your iDVD projects.
Keychain Scripting
Keychain Scripting is a new scripting addition in /System/Library/ScriptingAdditions. It implements the Keychain Suite with commands to Lock and Unlock a keychain, and classes for Keychain, Key, AppleShare Key, Internet Key and Generic Key objects. It also implements a few Standard Suite commands and the Application class.
loginwindow 2.0.0
The loginwindow application lives in /System/Library/CoreServices. It doesn’t have a dictionary, but it does respond to Apple events. Indications are that it provides some scriptability services to the System Events application (for example, in an earlier release a bug sometimes caused commands issued to the System Events application to return the name of the loginwindow application).
I don’t know the full panoply of events to which it responds, but in Mac OS X 10.0 and newer it responds to the Log Out (‘logo’) event and, in Mac OS X 10.2, to the Really Log Out (‘rlgo’) event, both declared in the AERegistry.h header file. Because of the absence of a dictionary, you must send raw events to the application to use them. Placing <> in a ‘tell application loginwindow’ block and running the script causes a log out window to appear, and in Mac OS X 10.2 the computer logs out after two mintues if the user doesn’t intervene. Placing <> in a ‘tell application loginwindow’ block and running the script causes the computer to log out immediately without a dialog, but only if you also enclose the raw Apple event in an Ignoring Application Responses’ block otherwise, the computer won’t be able to log out until the script quits, and the script won’t be able to quit until the log out event signals completion.
Mail 1.2
Jaguar’s Mail application does not include new dictionary terminology, but Jaguar does come with new scripts in the Mail Scripts subfolder in /Library/Scripts, including scripts for importing addresses to Address Book. They appear in the Script Menu if you install it in your menu bar as described above.
Apple’s legal department must have been working overtime when Apple issued AppleCare KnowledgeBase Document 106996 shortly before the official release of Jaguar, containing this topic heading: You may use scripts in the Mail Scripts folder to import addresses or for other purposes, but they are provided AS IS. Apple does not offer technical support for these scripts.
Print Center 2.0
Print Center, in /Applications/Utilities, has become a Cocoa application. It still contains the Print Center (with a space) Suite containing three important objects: Printer, Job, and Application. These allow you to get important information about all the printers attached to your computer, including those available via printer sharing, as well as the identity of the current default printer. Among other things, you can get the current status of any printer, such as whether it is idle or printing, and a list of all the jobs queued up on any printer. The Printer class has even been expanded with a new property, Status Description. But Print Center has lost its PrintCenter (without a space) Suite, which used to contain the commands and actions Show Queue, Queue Error, Print Dialog Browser, Print Job Started, and Test Event, some of which were designed to detect when the printer began doing various things.
QuickTime Player 6.0.1
QuickTime Player 6 was introduced more than a month before Jaguar, as a separate product, and it is included in the /Applications folder in Mac OS X 10.2. Its scripting dictionary contains many new commands and classes too many to enumerate here. All of the old commands and classes appear to be intact. People who use AppleScript to control QuickTime will want to study the improvements closely.
Sherlock 3
Sherlock, in the /Applications folder, is not only all new in Mac OS X 10.2, but its role has changed dramatically. It no longer has responsibility for searching your local drives for files and their contents that function has been taken over by the new Find capability accessible via the Finder but it has enhanced abilities to find and open various Web-based information repositories, such as movies playing at a theater near you. It is now known as Sherlock 3; while the version number of the old Sherlock was 3.2, that was Sherlock 2, an older product.
Sherlock 3 is another newly Cocoa-ized application, and it has the usual Cocoa Standard Suite and Text Suite. It retains the former Find suite, but it is the same only in name. The former Search Internet command is still there, but according to the dictionary it now takes a reference instead of a list of strings as its direct parameter (this is an artifact of the way Cocoa dictionaries are currently formed; a list of strings will still work); the former In Channel optional parameter has become simply the In optional parameter; and the former Using optional parameter for saved searches is gone. The Channel class still has a Name property, but it has lost its All Search Sites property while gaining an ID property to serve as a channel’s identifier. The Application class has shrunk, retaining only the Current Channel property. The Select Search Sites command is gone along with the All Search Sites property.
The dictionary doesn’t say so, but the direct parameter is optional. The dictionary also doesn’t disclose what result is returned, but experimentation reveals that it is a list of URLs as strings, as the dictionary used to state explicitly.
Standard Additions 1.9
Standard Additions is in /System/Library/ScriptingAdditions. It has been slightly enhanced in Jaguar.
Do Shell Script now takes an optional Boolean parameter, Altering Line Endings, to turn off translation of line endings to carriage returns.
Say now takes an optional parameter Saving To, as discussed above.
Standard Additions also contains the new Folder Actions suite with the commands familiar from the classic Mac OS, as discussed above.
System Events
As discussed above, the System Events application in /System/Library/CoreServices/ has acquired many new dictionary items. Prior to Jaguar, Apple cautioned scripters to direct System Events AppleScript commands to the Finder, since System Events only included Finder-related commands. Apple’s advice has now been reversed, because System Events has now become a central focus for many scriptable features of the system available nowhere else. In addition, commands addressed to System Events reportedly run much faster in Jaguar than they did in the last release.
First, it now has the telltale Standard Suite and Text Suite of a scriptable Cocoa application.
In addition, it has a new Login Items Suite mentioned above, containing Login Item and Application classes that allow you to add and remove items from the Login Items list in System Preferences. This is the first entry in a planned long list of suites for scripting system preference panes.
It also has a new Disk-Folder-File Suite with Folder, Item, File, Disk, and Application classes apparently taken from the Finder as part of the trend to move file system scripting out of the Finder.
It includes the Folder Actions Suite with some old and some new Folder Action commands and classes.
A new Power Suite includes commands to Shut Down, Restart, and Sleep, and an associated Application class. The AppleScript 1.9.0 for Mac OS X Release Notes indicate, incorrectly, that it also includes a command to Log Out, but the loginwindow application can be scripted, using raw Apple events, to log out as described above. Due to its Cocoa underpinnings, the System Events dictionary for the Shut Down, Restart, and Sleep commands, and others, indicates that a direct parameter is required, when in fact internally none is required. To make these commands work, use any reference as a dummy direct parameter, such as a string (as in ‘restart now’). The Release Notes also indicate that these commands now automatically work in an implied Ignoring Application Responses block. This is true when you run the script as an applet; when run within Script Editor without an explicit Ignoring Application Responses block, they work only after you quit Script Editor.
A new Processes Suite includes several classes relating to processes, apparently taken from the Finder. This suite includes a new Classic property so you can tell whether a process is running in the Classic environment of Mac OS X.
Finally, a new System Events (plural) Suite contains a Do Script command and an application class. The old System Event (singular) suite is gone.
System Preferences 2.1
System Preferences, once listed as scriptable but later yanked from the list, is once again listed as scriptable this time, for real. In this release, scriptability is limited. You can count the panes, list them, list their names, and get the current pane. The Current Pane command has one awkward feature: it returns no result (instead of an empty string or Missing Value) if all panes are showing. It is located in the /Applications folder.
Scripting the System Preferences application requires it to launch. Apple plans to implement most preference pane scriptability in the System Events application so that launching System Preferences will not be necessary to script them.
Terminal 1.3
Terminal, providing the UNIX command line interface (CLI) for Mac OS X, contains two new parameters to the Do Script command in Mac OS X 10.2. The optional In parameter lets you specify the window in which to execute the command. Doing so overrides the default behavior, which is to spawn a new Terminal window. It also takes a direct parameter now: the data to be passed to the Terminal application as a command line. The With Command optional parameter is still there for the same purpose, but it is now marked as deprecated. Terminal is in the /Applications folder.
Apple’s Chris Nebel explained that the new terminology makes it easier to issue a command to Terminal and then respond to a resulting Terminal prompt by issuing a second command in the same window, with a delay command inserted between the two to deal with potential timing issues.