NEW FEATURES, CHANGES, AND LIMITATIONS IN APPLESCRIPT 1.6
Several new AppleScript features, changes to existing features, and bug fixes are introduced in Mac OS X. Many of these apply across the board to the Mac OS X and Classic environments and to Mac OS 9.1, as well. Several scripting additions and other scriptable items that are installed as part of Mac OS 9.1 have not yet been included in the Mac OS X environment, however, although many soon will be added. On the flip side of the coin, there are a few new AppleScript-related items that are available only in the Mac OS X environment.
New Features
The dictionary in Finder 10.0 contains a number of new features, but for the time being, the Finder’s dictionary is explicitly described as provisional in both environments of Mac OS X. Some dictionary items are not yet hooked up and others do not work correctly. These gaps should be filled shortly, perhaps by the time of the promised summer update to Mac OS X. See The Scriptable Finder for more information.
The Standard Additions scripting addition is updated to version 1.6 and has changed in some respects in Mac OS X. Most importantly, it defines a new System Attribute command in the Mac OS X environment, replacing the Computer command formerly implemented in the Finder’s dictionary. Apart from the name change and the fact that you no longer need to call it in a Finder Tell block, the System Attribute command’s parameters and usage are identical. In fact, when you compile a script in the Mac OS X environment that contains the Computer command, whether in a Finder Tell block or on its own, it automatically changes to a System Attribute command. Scripts compiled in either the Mac OS X environment or Mac OS 9.1 or earlier using the name for the command that is available in that version of the system will run under the other without recompiling, provided that the Finder Tell block is included for scripts running in Mac OS 9.1 or earlier. You are left to remove the ‘tell application Finder’ wrapper yourself from scripts originally written for Mac OS 9.1 or earlier, if you wish to, although if left in place it is harmless when the script runs under Mac OS X and will provide backward compatibility with the old Computer command when your script runs under Mac OS 9.1 or earlier. (There is one fly in the ointment, however. The new System Attribute command is unique to the Mac OS X environment; it is not present in the Mac OS 9.1 and Classic versions of Standard Additions 1.6. This will lead to serious problems for some older scripts, because the Finder’s Computer command is also unavailable in the Classic environment. See Testing Whether AppleScript 1.6 is Installed for more information about this and another bug associated with the new System Attribute command and the elimination of the Finder’s Computer command.) For other amendments to the Standard Additions dictionary, see Dictionary Changes.
Many new features are functional or available only in the Mac OS X environment, such as new constants for the Path To command in Standard Additions, and new scriptable applications. See Where AppleScript Items Are Located and AppleScript Tools, Scripting Additions, and Scriptable Applications for information about them.
Changes
There are a number of changes to the scripting experience in Mac OS X, in addition to the new features described above and the limitations described below.
Users of Mac OS X Public Beta will notice that the Finder is now once again called the Finder, not Desktop (or Desktop.app) as it was in Public Beta. The Finder’s dictionary contains a few changes since Mac OS 9.1, both in the Classic and the Mac OS X environments, as dicussed below in The Scriptable Finder.
Users of Mac OS X Public Beta will be pleased to know that recording is now available using Script Editor, for applications that support recording.
Instead of using the classic Mac OS Startup Items folder to launch an AppleScript at boot time, place the script application anywhere you like, open the Login pane in System Preferences, click the Login Items tab, and assign the script to run at startup time. I haven’t found a way to run a script at restart, shutdown or log out time, however. The Execution State property of the Finder’s Application class, introduced in Mac OS 9.1, is gone in Mac OS X, so you can’t run a stay-open applet to check the execution state periodically.
To write a script for use in Mac OS X that uses terms from a Classic environment scripting addition, a topic in the Help Center explains that you have to make use of the Using Terms From command. According to this topic, you enclose the scripting addition commands in a Using Terms From block to allow them to compile using the Classic scripting addition’s terminology, and enclose the Using Terms From block in a Tell block directed at a scriptable Classic environment application. This will force the scripting addition command to execute in Classic, because scripting additions always run in the same environment (Classic or Mac OS X) in which the application addressed in the enclosing Tell block is running. (If you have scripting additions with both Classic and Mac OS X counterparts that behave differently in the two environments, you could use this technique to force the script to use one or the other counterpart.) Many, including me, were unable to make this recommended technique work until two alternative solutions appeared on the AppleScript mailing lists:
In two steps: First, put the scripting addition commands in a Using Terms From block, addressing an alias to the scripting addition using its path in the classic Mac OS Scripting Additions folder, and compile. Then remove the Using Terms From wrapper from your script (leaving the innards of the block in place, with their raw event codes) and replace it with a Tell Application wrapper, and recompile.
In one step: Put your scripting addition statements in a Using Terms From block, addressing an alias to the scripting addition using its path in the classic Mac OS Scripting Additions folder. Wrap the Using Terms From block in a Tell Application block, using a variable containing the name of the application instead of the name itself. Before the Tell Application block, insert a script command to launch any classic Mac OS application using its path as a hard-coded string (since the Path To command won’t let you find the classic Scripting Additions folder when running in Mac OS X). Compile. 9/22/01
If you examine the scripts that come with Mac OS X, you will notice that many of them carry a .scpt suffix, or file extension, in their names. AppleScript and Script Editor work both with these scripts (which have their dictionaries in their data fork, instead of in a resource fork) and with existing scripts that lack the file extension. You do not have to add the file extension to existing scripts, nor to scripts you create using Script Editor in Mac OS X. In general, I am told that Script Editor will honor .scpt for compiled script files, .applescript for uncompiled text, and .osax and .asdictionary for terminology. 9/22/01
The Dialect pop-up menu has been removed from Script Editor 1.6, since dialects other than English were removed in AppleScript 1.3.
One thing that has not changed is the format of file paths in AppleScript: they are still delimited by colons. Apple’s new AppleScript 10.0 Overview gives an example handler to convert an AppleScript path to a path using the Mac OS X-standard forward slash delimiter.
Limitations
The Mac OS X environment currently lacks many of the scripting additions and other scriptable items on which scripters have relied in the past. Although this will limit the tasks that can be performed by AppleScript for now, it is expected that many of these gaps will be filled in the promised summer release of the Mac OS X update, if not sooner. The items that are missing, or whose scriptability is missing, for the time being from the Mac OS X environment are listed below, followed by discussion of other limitations on AppleScript in Mac OS X.
Scripting additions: AirPort Scripting (introduced very recently with the AirPort 1.3 upgrade for Mac OS 9.1), Desktop Printer Manager (but see Mac OS X Print Center), FileSharing Commands, Keyboard Addition, Keychain Scripting, MonitorDepth (but see QuickTime Player), Network Setup Scripting, Sound Scripting, and Speech Listener.
System extensions: Application Switcher, Folder Actions, and FontSync Extension.
Control panel applications: Appearance, Apple Data Detectors, Apple Menu Options, File Exchange, File Sharing, FontSync, Location Manager, Memory, Mouse, Startup Disk, USB Printer Sharing, and Web Sharing. Two of these, USB Printer Sharing and Web Sharing, were minimally scriptable in Mac OS 9.1 only as an accidental artifact of the development system that was used to create them.
Applications: Apple Applet Runner, Apple Verifier, Apple Video Player, Disk Copy, Disk First Aid, and WorldText (but WorldText is available on the Mac OS X Developer Tools CD, and Mac OS X includes its own scriptable TextEdit application). One of these, WorldText, was minimally scriptable in Mac OS 9.1 only as an accidental artifact of the development system that was used to create it.
Limitations on AppleScript user interaction commands, such as Display Dialog, are discussed extensively, with workaround strategies, in Apple’s new AppleScript 10.0 Overview. This issue requires care and testing of your scripts that employ user interaction commands, but the change in scripting style required to deal with this limitation is not difficult to master. Basically, AppleScript-generated dialogs and windows will appear only in applications that are able to execute scripts, such as Script Runner and AppleScript applets, or in the application that is running the script, such as Script Editor. In the latter case, telling the application target of the Tell block to activate will not bring the application that is running the script to the foreground, and, unless you employ a workaround, the user may therefore have to bring it to the foreground manually to see the dialog or window. Rather than repeat the workarounds here, I urge you to read Apple’s discussion. For example, you can enclose user interaction commands in a Tell Me block, or move them to a separate handler that executes outside of the Tell Application block. The AppleScript 10.0 Overview gives many other examples. Users of older versions of Leonard Rosenthol’s OSA Menu will find some of this familiar. Apple expects to address this limitation in a future release.
In Mac OS X, traditional Macintosh resource-based file types are optional. Some documents do not have such resources, but depend instead on file name extensions like .jpg and .tif. This will at first be very uncomfortable to Mac users, who tend to scorn file extensions as an MS-DOS antique, but we will have to get used to them. Indeed, Apple is encouraging the use of file extensions because they confer compatibility benefits in the Internet context and elsewhere. This has an important impact on scripters. When using the Info For command to check a file’s type, you must hereafter also check for a file extension. The AppleScript 10.0 Overview gives examples of how to parse a file extension. When using the Choose File command, you should follow a similar strategy.
Although scripts running in either environment of Mac OS X can control scriptable applications running in the other environment, the Classic Script Editor cannot see the dictionaries of Mac OS X applications, such as Mail, that are bundles, because it thinks they are folders. Other Mac OS X applications, such as Carbonized applications based on the traditional single-file, dual-fork application model, are visible to the Classic Script Editor.
Some items that are scriptable in Mac OS X are not available at all in the Classic environment, much less scriptable there. For example, many Classic control panels are not available because the Classic environment uses Mac OS X settings. Since the Mac OS X System Preferences application is not scriptable in this release, there are therefore some things you can’t do with AppleScript even in the Classic environment, which you could do in Mac OS 9.1 and earlier. For example, the Network Setup Scripting scripting addition application will not work in the Classic environment. In terms of sheer numbers, this limitation will probably have the greatest impact on existing scripts.
In Mac OS X, the Apple Event Manager does not currently support sending events to remote machines. Issues with the user interface and security models have not yet been resolved, and Program Linking is not supported. In addition to being unable to specify a remote application in a Tell block, you cannot open dictionaries of remote applications or select remote applications when choosing an application.
AppleScript does not support preemptive threads under Mac OS X (and never did under the classic Mac OS).
Mac OS X does not currently come with a scheduling application equivalent to the iDo Script Scheduler utility that ships with Mac OS 9.1. You can, however, explore the capabilities of the Unix cron command, which is included with Mac OS X; type man cron in the Terminal application for instructions. Since the release of Mac OS X, at least two utilities have appeared that do perform a scheduling function: CronniX 1.2, an application from Sven Schmidt that puts a user interface on the cron command (and thus has nothing to do explicitly with AppleScript, although it can be used to launch applets on a schedule), and Script Timer 1.1X, an application from Donald Hall that launches AppleScript and other OSA scripts according to any schedule. 7/13/01
Apple Data Detectors are not included in Mac OS X, but it is likely that the Mac OS X Services feature will eventually fill a similar role.
Folder actions are not implemented in the current release of Mac OS X, although the new Standard Additions 1.6 still includes the folder actions commands. Expect to see them in a future release.
When using the Classic Script Editor to open or compile a script, it may fail at first because some applications have to launch in order to disclose their dictionaries. Give it a little more time, then try again.
Volumes configured as UFS or NFS volumes will not work correctly with the Info For command in Standard Additions. Most users are advised to format their disks as HFS Plus, or Mac OS Extended, volumes, in any event, so this will not affect many scripters.
The Path To and Info For commands from Standard Additions may return unexpected results in some cases. Some Mac OS X applications are not traditional application files, but bundles (just as a few Mac OS 9 and later classic applications are packages). A bundle (or package) is a folder containing a number of other items, including subfolders and files, but the Finder and other savvy applications display them as if they were files. The Path To command, when addressed to such an application, will usually return the name of the application with the .app file extension. The Info For command will usually return true for the Folder property, and the File Type, File Creator and some other properties will not be available. Hopefully, these issues will be resolved in a future release of Mac OS X.
NEW APPLESCRIPT FEATURES IN MAC OS 9.2.1 9/22/01
The release of Mac OS 9.2.1 on August 21, 2001 followed a more tortuous path than usual. Initially, the original edition of Mac OS 9.2, having a code name of Moonlight, was completed in the summer of 2001 but was never shipped. Later, the summer 2001 iMacs and perhaps some other new machines shipped with an updated Mac OS 9.2, codenamed Starlight. Finally, on August 21, Mac OS 9.2.1 was released to the public, codenamed Limelight. Or so the rumors would have it the code names are certainly evocative.
Mac OS 9.2.1 was described as a bug-fix release, intended to help bring the classic Mac OS to its end of life in good shape for long term use on older machines and to provide better compatibility with the Classic environment of Mac OS X. Nevertheless, there are a few new AppleScript-related features included in it, even though AppleScript itself and the Script Editor remain at version 1.6.
The Keychain Scripting faceless background application is at version 2.4, up from 2.2. Its dictionary includes a new Run command as the flip side of its existing Quit command. The Creator Code property of class Key has been changed to Creator Type to bring it into conformity with common usage in other contexts. Finally, class AppleShare Key has a new property, Address, which is described as the DNS host name or IP address for servers on a TCP/IP network.
The Network Setup Scripting faceless background application is at version 1.3.1, up from 1.2. It includes a new property in the Remote Access Suite, Answers Calls, to turn on or off incoming calls to Remote Access server if installed and not locked.
The Sound Scripting faceless background application is at version 8.5.6, up from 8.5.4. The Set command has been removed, consistently with the overhaul of AppleScript terminology some time ago. However, this item has not been given a new Run command, as was Keychain Scripting. The former Settings class is now class Application, and several properties have been renamed for the sake of clarity: Alert Sound to Current Alert Sound, Sound Input Device to Current Sound Input Device, Sound Input Source to Current Sound Input Source, and Sound Ouput Devices to Current Sound Output Devices.
The FontSync Extension is at version 2.0, up from 1.0, as part of an extensive overhaul of the FontSync technology. The Set and Get commands were removed as part of the ongoing campaign to overhaul AppleScript terminology. The existing Create Font Profile command has two new parameters, Using Font Reference Version and Font Ref Version.
Finally, the following scriptable items were upgraded without changing their dictionaries: Startup Disk, URL Access Scripting, and Folder Actions.
The AppleScript Help module in Apple Help still carries (at least on my computer) a What’s New section describing changes in AppleScript 1.5.5.
DOCUMENTATION FOR NEW APPLESCRIPT FEATURES
The HTML-based Apple Help system is alive and well in Mac OS X, and it contains some information relevant to AppleScript. Open Mac Help from the Help menu of almost any Mac OS X application and search for the word AppleScript, or open the Help Center and choose AppleScript Help. You will be invited to go to a section where you can Learn what’s new with this release of AppleScript, and to read topics entitled What is AppleScript?, How do I use Script Editor?, and How do I start writing scripts? Ask also about other topics, such as Automating color management tasks and Script Runner. Topics requested in Apple Help under Mac OS X are quite often downloaded from Apple’s web site, so the information should always be up to date.
Shortly after the release of AppleScript 1.6, Apple published Tech Info Library article 60835, AppleScript 1.6: What’s New and Different from AppleScript 1.5.5. 9/22/01
Apple’s AppleScript in Mac OS X overview provides extensive information about new AppleScript features in Mac OS X 10.0, along with some sample scripts and an e-mail address to which you are requested to send feedback (i.e., bug reports and feature requests) specifically related to AppleScript in Mac OS X: OSX_AppleScript_feedback AT apple.com. The documentation is also provided in the form of a downloadable Mac OS X Help Center module called AppleScript 10.0 Overview so that you can have it available at all times when using Mac OS X.