NEW FEATURES IN APPLESCRIPT 1.3
AppleScript 1.3 represents a major advance in AppleScript’s development. These new features are notable:
AppleScript is now PowerPC native, resulting in an estimated threefold to fivefold improvement in execution speed of basic AppleScript commands. This, combined with the immense increase in hardware speed in the new G3 Macs, makes for a radically different user experience when running scripts. The About AppleScript document indicates that AppleScript 1.3 cannot be used with versions of the operating system older than Mac OS 8.5. Furthermore, because it is native, it cannot be used on 680x0 Macintosh computers (which cannot in any event run Mac OS 8.5). Note that the speed increase does not extend to Apple events sent to applications, because the Event Manager remains mostly emulated 680x0 code.
Paraphrasing a summary of the speed issue by Andy Bachorski of Apple: The Apple Event Manager (AEM) and Process Manager are 68K. The AppleScript (AS) OSA component is native. AS only used the AEM to send events to other processes. Other than that it doesn’t rely on the AEM for anything else. Many of the osaxen in the Standard Additions scripting addition are native, though not all. Osaxen are loaded by AS, but called by the AEM. Coercion handlers, again loaded by AS and called by the AEM, are a mixed bag, with the unit types being 68K code (so trivial they wasted time being native) and Unicode coercions being native, as examples. The overall performance of AppleScript is improved, especially any scripts which do a lot of their own work (numeric calculations, etc.), though you can find specific cases where native AppleScript is a bit slower than the non-native version. 10/10/99
The terminology dictionaries of basic system scripting facilities have been reorganized to reflect a more logical organization. This change is particularly evident in the scriptable Finder’s dictionary. The old Required Suite of four basic commands has been eliminated and their commands distributed among other suites, and new functional categories now organize commands and object classes into a more readable and understandable form.
A new Scripts folder exists at the root level of the System Folder, as an optional standardized location for users’ scripts. A new Folder Action Scripts subfolder appears here automatically, for example, as an optional location to place folder action scripts. The Scripts folder has long been known to users of Leonard Rosenthol’s OSA Menu, a utility that places a Scripts menu in the menubar. OSA Menu Lite is now provided as an optional feature of the operating system.
Many more features of the operating system have become scriptable. These now include the following control panels, system extensions, applications and feature-specific scripting additions: Appearance, Apple Data Detectors Scripting, Apple Help Viewer, Apple IR File Exchange, Apple Menu Options, Apple System Profiler, Apple Video Player, Application Switcher, ColorSync, ColorSync Extension, Desktop Printer Manager, Desktop Printing, Disk Copy, Disk First Aid, File Exchange, File Sharing, File Sharing Commands, Find (Sherlock), Folder Actions, Internet, Keyboards, Keyboard Addition, Location Manager, MonitorDepth, Network Setup Scripting, OSA Menu, Remote Access Commands, Set Volume, SNMP Administrator, Users & Groups, Web Sharing and Web Sharing Extension. And, of course, the Finder. In an interesting new twist, some of the features of some of the new system components are available only through an AppleScript interface. The Application Switcher is a notable example of this, and it has spawned a dozen free and shareware applications based on Apple events. Apple’s official AppleScript web site contains Mac OS 8.5 Scripting Overview, a growing collection of articles and example scripts for scripting these scriptable system components.
Third-party software distributed by Apple on the system installation CD-ROM is also scriptable. Examples include Iomega Tools, StuffIt Expander and, of course, Microsoft Internet Explorer and Microsoft Outlook Express.
Several items in Mac OS 8.5 have scripting dictionaries (terminology, or ‘aete’ resources) but are not in fact scriptable. I understand this is an artifact of the development system used to create them. They include: the Apple Data Detectors control panel.
Many formerly separate scripting additions have been consolidated into a single Standard Additions scripting addition, which also now includes new commands. The old separate scripting additions have the same interface and functionality in the new location (except for the addition of the new timeout parameter to ‘display dialog’). The following formerly separate scripting additions have been incorporated into Standard Additions and should be removed from your Scripting Additions folder if present: AGStart, Beep, Choose Application, Choose File, Current Date, Display Dialog, File Commands, Load Script, Numerics, String Commands, Read/Write Commands, Run Script, Store Script and Time To GMT.
Several new commands have been added to existing scripting additions. These include the clipboard commands from the popular Jon’s Commands scripting addition; the ‘delay’ command to pause script execution without stealing processor cycles from other operations; the ‘choose from list’ command to display and select from a user-defined list of text items, from the popular and venerable GTQ Scripting Additions package; the ability to mount remote volumes from Leonard Rosenthol’s MountVolume scripting addition; the ‘say’ command for speech synthesis that was formerly an optional install as part of Apple’s Text-to-Speech software; a command to summarize complex text into an arbitrarily small number of sentences; and the ability to display dialogs that go away automatically after a user-defined interval. In addition, plain English terminology has been provided for a number of scripting commands which formerly required use of obscure raw Apple event codes. These include the ‘reopen’ event and three new terminology suites for Internet data types, Mac OS connectivity and folder actions. Some of these allow Common Gateway Interface (cgi) scripting on web servers using plain English. Many new selectors are added for the ‘path to’ command that is now part of the Standard Additions scripting addition. Some existing commands may have new names after you compile existing scripts under Mac OS 8.5, but they will work the same. A few existing scripts may require rewriting.
Units of measure can be used to convert between measurement systems. For example, ‘x as meters’ can be converted to feet simply by writing ‘x as feet’. Lawrence D’Oliveiro has published his thoughts on Mac OS 8.5 units. Andy Bachorski of Apple has described the units feature as follows, paraphrasing: The implementation of unit types in AppleScript is very simplistic and was the best that could be done given the current limitations of the language. It was never intended to be used as a tool for doing higher mathematics, but rather as a tool that would allow script writers to designate a unit of measure to be associated with a value which can then be sent to an application where that unit is interpreted, scaled, and applied to the target object. Responding to a mathematician’s suggestions, Andy decscribed the syntax as follows: Unfortunately, there is no direct support for units in AppleScript, which prevents us from using pure mathematical syntax. An alternative syntax is also supported, though, with somewhat limited functionality. That syntax is ‘set x to meter: 12’. With the current implementation you can write statements like ‘x as cm’, which will perform the proper scaling. This will work for other like (length to length, volume to volume) unit types. The conversion goes through the base type, so when you write something like ‘set x to 12 as meter’ you create a variable of type meter containing a real number of value 12.0. When you write ‘set y to x as inches’, a coercion handler will perform any scaling needed to convert from meters to inches, which essentially means that it will multiply the meters value of 12 by 39.0625. If you then write ‘set z to y as feet’, the inches value in y is converted back to meters, then scaled as needed to get the number of feet in 12 meters… The biggest limitation to the current scheme is that it is up to the script writer to make sure that different unit types are of the same base before using them in calculation. It makes script writing a bit cumbersome, but it was felt that the benefits outweighed the limitations. 10/10/99
Unicode text is said to be fully supported. Technote 1142 suggests this means only that the class ‘Unicode text’ is recognized as a stream of Unicode text, which can be coerced to and from the ‘text’, ‘styled text’ and ‘international text’ classes. For example, the AppleScript statement ‘Bill as Unicode text’ returns the value ‘<<data utxt00420069006C006C>>’. The statement ‘class of <<data utxt00420069006C006C>>’ returns Unicode text. And the statement ‘<<data utxt00420069006C006C>> as text’ returns Bill. A Unicode-savvy application like Style, the scriptable text processor by Marco Piovanelli, inserts the word Bill into a document when you run this statement: ‘tell application Style to set beginning of front document to <<data utxt00420069006C006C>>’. Marco has described his discoveries regarding Unicode in AppleScript as follows: While exploring the new Unicode capabilities in Allegro, I stumbled into this definition: typeStyledUnicodeText = FOUR_CHAR_CODE(‘sutx’) (from version 3.2 of <AERegistry.h>). Interesting. Let’s see if AppleScript understands this. In the Script Editor, the following line: <> compiles to: styled Unicode text So ‘sutx’ was added to the 8.5 global aeut. Unfortunately, there doesn’t seem to be a systemwide coercion handler from STXT (styled text) to sutx, while there is a coercion handler from TEXT to utxt (Unicode text). (BTW, you can list system coercion handlers using my aeinfo dcmd). Too bad. I imagine a ‘sutx’ descriptor is analogous to a ‘STXT’ descriptor, i.e., a coerced AE record with keys keyAEText (or its equivalent for Unicode) and keyAEStyles. Marco’s findings are confirmed in the AppleScript 1.3.4 SDK. 10/10/99
Folder actions are implemented, as a first step toward making the Finder attachable.
On the negative side, only the English scripting dialect is supported in AppleScript 1.3.
In Mac OS 8.5, you can use an idle handler to check when a file copy has completed. From Andy Bachorski of Apple: For the case where the Finder is copying a file, you can check the file type and creator type of the file you are interested in. While the Finder is copying a file, it will have a creator code of ‘MACS’ and a file type of 'bzy ’ [Note the space after ‘bzy’]. You would write your script to wait until the file no longer has these codes to know when the copy is finished. For an example of how to implement this feature using an idle handler, see the Wait for Copy script. 10/10/99
The Finder also implements a new ‘as alias list’ coercion, designed to let you obtain very rapidly a list of aliases to Finder items meeting scripted criteria, in addition to the list of Finder references that has heretofore been available. Jon Pugh objects to this new construct because it is not a generalized solution to the problem of how to get a list of aliases. Paraphrasing Jon in an October 3, 1998 communication to the MacScripting mailing list, The original problem was this:
tell application "Finder"
set x to items of folder foo as alias
The workaround is this (we’ll skip the tell block from here on):
set x to items of folder foo as alias
on error number -1700 from y
set x to y
Now, the kludge is this:
set x to items of folder foo as alias list
So, what happens if you do this?
set x to items of folder foo as string
There isn’t a string list, so you still need to use the old workaround and the problem isn’t really fixed. It’s just got a band-aid on it. 10/10/99
With Mac OS 8.5, Apple has published a number of scripts that cleverly make Apple’s Script Editor scriptable after a fashion. They belong in a Script Editor subfolder in the Scripts folder, so that OSA Menu or OSA Menu Lite will list them in the Scripts menu when the Script Editor is frontmost. On October 19, 1998, Jon Pugh suggested another trick, making it easy to open frequently-used terminology dictionaries from OSA Menu. Put this script in a Script Editor subfolder in the Scripts folder in your System Folder:
property a : alias Macintosh HD:System Folder:Finder
tell application "Script Editor"
Then do the same for all of your scriptable applications. To make the task easier, write a Finder script that gets the selection as an alias, copies the script above to a new file based on the alias file name, loads the script, sets property a to the alias and stores the script back. This will allow you to create new custom dictionary opening scripts from the Finder. 10/10/99