Applescript 2.0 (Mac OS X 10.5 'Leopard ')

Application Objects

Lots of changes in the handling of application objects! Some have been needed for a long time, others were unexpected, but useful.

New properties
Application objects now have a running boolean property that lets you test for a running application without resorting to System Events. You can also test whether an application is frontmost using the frontmost property. Lastly, you can get an application’s version property without having to activate the application or send it any commands.

New behaviors
You can now target an application by its bundle identifier (“com.apple.mail” for example) or creator type (“emal”). This is useful for applications whose name may include a version number that changes from version to version. The phrases application “Mail”, application id “com.apple.mail” and application id “emal” are all valid references. This means that there is new id property for applications, so if you use set appID to id of application “Mail” you will get “com.apple.mail” in appID.

If you refer to applications by id rather than name, the app will continue to be found by Applescript even if the user has changed its name. But if you use this form and the application can’t be found, Applescript won’t ask the user to find it, it will throw an error (which you can catch with a try statement).

Other changes: Applications referenced in a tell block will launch hidden when commands are directed to them unless you specifically use launch or activate. Applications are located “lazily” which means they aren’t looked for until the script execution hits a tell block. This also means that applications are re-found each time they are referenced in a tell block, so that if an application is moved it will be found each time it is referenced. Apple’s release notes also say:

Apple notes that these new properties won’t cause errors with most applications in older versions of Applescript - Applescript will send the events to an application rather than handling them, assuming that they are application events. The release notes also say that most apps will handle name, version, and frontmost just fine in previous versions of Applescript, but that support for id or running would be rare.

Scriptability Issues

Network preferences are now scriptable via System Events. This means that you can use Applescript to change your network prefs, handy for laptops that connect to more than one network. Apple provides the following example:


tell application "System Events"
	tell network preferences
		tell current location
			set aPPPoEService to a reference to (first service whose kind is 10)
			if exists aPPPoEService then
				connect aPPPoEService
			end if
		end tell
	end tell
end tell

In addition, both the Applescript Utility and Folder Action Setup are now scriptable, which means that administrators can write scripts to create folder actions and turn on Utility options. Script Editor’s scriptability has been enhanced as well, and all 3 applications are now VoiceOver aware.

Command Line Changes

The # character is now usable as a “comment to end of line” symbol, acting the same as the symbol. This allows scripts to be executed like unix shell scripts, provided that the first line of the applescript is set to #!/usr/bin/osascript and the file’s execute permission is turned on. Scripts that include the # character and are compiled under Applescript 2.0 will operate on older systems without problem.

You can display compiled scripts on the command line with the osadecompile command. The commands osascript and osacompile now handle UTF-8 encoded text scripts as well as UTF-16 and the primary encoding. If a script is of neither UTF type it is assumed to be in the primary encoding.

Enhancements

Script Editor
You can now see all the inherited properties and elements of an object in the dictionary viewer. You can turn this on in General preferences.

You can show the non-printing characters tab, carriage return, and linefeed in text constants as an escape sequence instead of a real tab, carriage return or linefeed, as it is now. This preference is in the Editing prefs.

path to me will return the Script Editor if a script hasn’t been saved yet. Otherwise, it returns the path to the script’s document file.

System Events
The properties server and zone have been added to disk objects for use with AppleShare.

File objects now have a default application property, similar to the info for’s default application or the Finder’s open with setting in Get Info.

System Events now has a downloads folder property.

System Events can control some Security preference settings with its new Security suite.

Two new properties have been added to the process class, bundle identifier and architecture, the latter specifying PowerPC or Intel (or some future architecture, it is assumed).

Property list items and files can be manipulated with the new Property List suite.

The name and id properties of an XML element object now reflect any name or id given in the XML code, and XML objects can be referenced using XML element id or XML element named syntax.

Image Events
RAW images are supported.

with pad color has been added to the pad command.

with compression level has been added to the save command for JPEG saving.

Bug Fixes

Here are the highlights. Here is a complete list of bugs that were fixed.

The delay command uses less CPU cycles.

Objects other than application and date specifiers will not be evaluated during compilation, but at run time. This is especially important for alias and POSIX file items.

Script memory usage is no longer limited to 32mb.

Raw data literals are no longer limited to 127 bytes.

A script may not tell a remote application to do shell script.

Script Editor’s handling of asian characters has been improved both when reporting errors or displaying applications in the Library window.

disk objects can tell the difference between disks with the same name.

file type and creator type work properly on Intel Macs.

The process class now reports false for the frontmost property if the front application is System Events or other UI element apps. That means that first process whose frontmost is true will be the same application as path to frontmost application.

Folder Actions have been improved and Folder Actions Setup allows a script bundle as an action.

Database Events’ performance is improved.

Developer Notes

The full developer notes are available here. Of note are that sdef’s can be read directly instead of launching the application to read them, and that there is a new sdef architecture.