This release note provides the latest information about AppleScript 2.0, which ships with Mac OS X version 10.5, as well as a list of the more prominent bug fixes. For older release notes, see AppleScript Release Notes (Mac OS X 10.4 and earlier).
AppleScript 2.0 can use scripts developed for any version of AppleScript from 1.1 through 1.10.7, any scripting addition created for AppleScript 1.5 or later for Mac OS X, and any scriptable application for Mac OS 7.1 or later. A script created with AppleScript 2.0 can be used by any version of AppleScript back to version 1.1, provided it does not use features of AppleScript, scripting additions, or scriptable applications that are unavailable in that version.
Unicode Support
Application Objects
Scriptable Network Preferences
Scriptability and VoiceOver
Command Line Support
Other Enhancements
Bug Fixes
Developer Notes
AppleScript is now entirely Unicode-based. Comments and text constants in scripts may contain any Unicode characters, and all text processing is done in Unicode, so all characters are preserved correctly regardless of the user’s language preferences. For example, this script works correctly in AppleScript 2.0, where it would not have in previous versions:
set jp to "日本語" |
set ru to "Русский" |
jp & " and " & ru -- returns "日本語 and Русский" |
There is no longer a distinction between Unicode and non-Unicode text. There is exactly one text class, named “text”: that is, class of "foo"
returns text
. It is functionally equivalent to the former Unicode text
class, so it may contain any Unicode character, and has two new features besides:
text
objects have an id
property, which may also be used as an address.
These allow mapping between Unicode code point values and the characters at those code points: for example, id of "A"
returns 65
, and character id 65
returns "A"
. The id of text longer than one code point is a list of integers, and vice versa: for example, id of "hello"
returns {104, 101, 108, 108, 111}
, and string id {104, 101, 108, 108, 111}
returns "hello"
. (Because of a bug, text id ...
does not work; you must use one of string
, Unicode text
, or character
.) These obsolete the older ASCII character
and ASCII number
commands, since, unlike those, they cover the full Unicode character range and will return the same results regardless of the user's language preferences. [2915643]
character
elements of text count a combining character sequence as a single character.
This relates to a feature of Unicode: some “characters” may be represented as either a single entity or as a base character plus a series of combining marks. For example, “é” may be encoded as either U+00E9 (LATIN SMALL LETTER E WITH ACUTE) or as U+0065 (LATIN SMALL LETTER E), U+0301 (COMBINING ACUTE ACCENT). Nonetheless, AppleScript 2.0 will count both as one character, where older versions counted the base character and combining mark separately. [4192557]
Matching of text item delimiters
now respects considering
and ignoring
attributes, where previous versions ignored them. The expansion
attribute is no longer supported.
The change to all-Unicode necessitated a change in the read
and write
commands and how they deal with text encodings. Formerly, they would write string
objects using the primary encoding and Unicode text
objects as UTF-16, unless instructed otherwise using an as
parameter. Now that there is only one text class, the read
and write
commands rely solely on the as
parameter to determine the encoding: with no as
parameter, as text
, or as string
, they use the primary encoding; with as Unicode text
, they use UTF-16. For the most reliable results when creating scripts that will run on both 2.0 and pre-2.0, always specify the encoding explicitly using as text
or as Unicode text
, as appropriate. [4421553]
For compatibility with pre-2.0 AppleScript, string
and Unicode text
are still defined, but are considered synonyms for text
. For example, all three of these statements have the same effect:
someObject as text |
someObject as string |
someObject as Unicode text |
In addition, text
, string
, and Unicode text
will all compare as equal. For example, class of "foo" is string
is true, even though class of "foo"
returns text
. It is still possible for applications to distinguish between the three different types, even though AppleScript itself does not.
Now that AppleScript preserves all characters correctly worldwide, it is also stricter about the text used in scripts. AppleScript syntax uses several non-ASCII characters, such as “≠” and “¬”. These characters must be typed exactly as the AppleScript Language Guide describes. For compatibility with Asian national encodings, “《” and “》” are allowed as synonyms for “«” and “»”, since the latter do not exist in some Asian encodings.
Because all text is Unicode text, scripts now always get the Unicode text behavior. This may be different from the former string
behavior for some locale-dependent operations, in particular word
elements. To get the same behavior with 2.0 and pre-2.0, add an explicit as Unicode text
coercion, for example, words of (someText as Unicode text)
.
Because text item delimiters
now respect considering
and ignoring
attributes, they now are case-insensitive by default. Formerly, they were always case-sensitive. To get the same behavior with 2.0 and pre-2.0, add an explicit considering case
statement.
Because AppleScript 2.0 scripts store all text as Unicode, any text constants count as a use of the former Unicode text
class, which will work with any version of AppleScript back to version 1.3. A script that contains Unicode-only characters such as Arabic or Thai will run, but will not be correctly editable using pre-2.0 AppleScript: the Unicode-only characters will be lost.
Use of the new id
property requires AppleScript 2.0.
application
objects in AppleScript have some new features designed to reduce the need for awkward or obscure workarounds. In other words, the capabilities are not new, but they are now directly supported and easier to use.
Is an application running?
While System Events is capable of telling you whether or not an application is running, it takes several lines to do it. application
objects now have a running
property that can give you the answer directly, without invoking System Events. For example, the following script pauses iTunes, but will not launch it if it is not already running:
tell application "iTunes" |
if it is running -- same thing as "if running" or "if running is true" |
pause |
end if |
end tell |
running
does not need to appear inside a tell block; the above script could also be written like this:
if application "iTunes" is running |
tell application "iTunes" to pause |
end if |
Is an application frontmost?
application
objects now have a frontmost
property similar to running
, including that the information is available from System Events, but it tells you whether or not the application is in front. The value of frontmost
for background-only applications, UI element applications such as System Events, and applications that are not running is always false
.
Get an application’s version
Most applications have a version property of their own, but using it requires that the application be running, and sometimes you specifically want to avoid that. The new built-in version
property will return the application version as text without launching the application or sending it an event. There is also a corresponding built-in name
property; the value is usable as the name of an application to tell
.
Target applications by signature or bundle identifier [3858040]
Doing this in older versions requires a multi-line incantation using Finder. It is now possible to simply address an application
object by id, where the id is either the bundle identifier or the four-character signature code. For example, application "Mail"
, application id "com.apple.mail"
, and application id "emal"
all refer to Mail.app, and will all work as the target of a tell
block, as in this example:
tell application id "com.apple.mail" |
get unread count of inbox |
end tell |
There is a corresponding id
property, so you can get the necessary id to tell:
get id of application "TextEdit" |
-- returns "com.apple.TextEdit" |
-- Now we know that 'tell application id "com.apple.TextEdit"' will work. |
This does not require that the application be running.
When running a script, if an application specified by id is not found, AppleScript will not ask where it is. It will throw an error, which can be caught using a try
block.
Scripts intended for distribution should use the id form. That way, the script will continue to work even if the user has changed the name of the application.
In addition, there are several changes to application behavior to make them easier to deal with:
Applications launch hidden.
AppleScript has always launched applications if it needed to in order to send them a command. However, they would always launch visibly, which could be visually disruptive. AppleScript now launches applications hidden by default. They will not be visible unless the script explicitly says otherwise using launch
or activate
.
Applications are located lazily.
When running a script, AppleScript will not attempt to locate an application until it needs to in order to send it a command. This means that a compiled script or script application may contain references to applications that do not exist on the user’s system, but AppleScript will not ask where the missing applications are until it encounters a relevant tell
block. Older versions of AppleScript would attempt to locate every referenced application before running the script.
When opening a script, AppleScript will attempt to locate all the referenced applications in the entire script, which may mean asking where one is. Pressing the Cancel button only cancels the search for that application; the script will continue opening normally, though custom terminology for that application will display as raw codes. In older versions, pressing Cancel would cancel opening the script.
Applications are located and re-located dynamically.
application
object specifiers, including those in tell
blocks, are evaluated every time the script runs. This alleviates problems with scripts getting “stuck” to a particular copy of an application. [4356296]
Uses of the built-in application properties will fall back to sending an event to the application in older versions of AppleScript, but the application may not handle them the same, or handle them at all. (Most applications will handle name
, version
, and frontmost
; id
and running
are uncommon.) The other new features above require AppleScript 2.0.
Networking preferences are now accessible via scripting using a new suite in System Events, where they were previously only scriptable via accessibility. A script can inspect various networking settings, and can tell a service to connect or disconnect. For example, the following script locates a PPPoE service in the current location and tells it to connect if it exists.
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 |
AppleScript Utility and Folder Actions Setup are now scriptable, and Script Editor’s scriptability has been enhanced thanks to improvements in Cocoa Scripting. All three applications are now VoiceOver-aware.
AppleScript now allows #
as a comment-to-end-of-line token, in addition to --
. This means that you can make a plain text AppleScript script into a Unix executable by beginning it with the line #!/usr/bin/osascript
and giving it execute permission. For details of osascript
usage, including how to access command line arguments from the script, see the osascript
(1) man page. [2468788]
There is a command-line tool to display compiled scripts as text, /usr/bin/osadecompile
. [4501123]
osascript
(1) and osacompile
(1) now correctly handle text scripts encoded as UTF-8, in addition to UTF-16 and the primary encoding. If a script is neither UTF-8 nor UTF-16, it is presumed to be encoded using the primary encoding.
Compiled scripts that use #
will run normally on pre-2.0 systems, and if edited will display using --
. Executable text scripts using #!/usr/bin/osascript
as above will not run on pre-2.0 systems, since the #
will be considered a syntax error.
The dictionary viewer allows class definitions to show all the properties and elements they inherit from other classes. To turn this on and off, use the “Show inherited items in dictionary viewer” setting in the General preferences. [4950321]
Script Editor can show tabs, carriage returns, and linefeeds in text constants as an escape sequence rather than a literal tab, carriage return, or linefeed. To turn this on and off, use the “Escape tabs and line breaks in strings” setting in the Editing preferences. [4911918]
When run from a script document in Script Editor, path to me
will return the location of the document file. If the document has not been saved, path to me
will return the location of Script Editor. [3148582, 5363659]
The disk
class has server
and zone
properties for use with AppleShare volumes. [3554247]
The file
class has a default application
property, corresponding to info for
's default application
property and Finder's open with
setting in Get Info. [4796981]
System Events has a downloads folder
property. [5255406]
System Events has a new Security Suite, which controls various settings from the Security preference pane. [4358335]
The process
class has a bundle identifier
property. [4782866]
The process
class has an architecture
property, which specifies the processor architecture of that process. [5237251]
The Property List Suite allows creating new property list files and property list items. [4728058, 5392953]
If a XML element
object has a “name” or “id” attribute, it will be reflected in the element's name
or id
property, respectively. XML element named ...
and XML element id ...
will also work. [4437103, 4441834]
Image Events supports RAW images. [4250666]
The pad
command has an optional with pad color
parameter. [5234464]
The save
command has an optional with compression level
parameter to specify the JPEG compression level. [3614780]
The delay
command uses less CPU. [3178086]
Impossible object specifiers in math expressions, such as 1 + character 2 of "9"
, produce an error instead of a random result. [4029175]
The numerical limits for repeat
loops accept real numbers; they will be rounded to integers. [4215670]
Object specifiers other than application
and date
specifiers, in particular alias "..."
and POSIX file "..."
, are not evaluated at compile time, and will be left exactly as originally typed. [4444698]
AppleScript no longer limits script memory usage to 32MB. [4511477]
Counting the paragraphs of an empty string gives a result of zero. [4588706]
Raw data literals («data ...»
) are no longer limited to 127 bytes. [4986420]
choose from list
correctly sizes the dialog if given more than 2000 items. [4102349]
display dialog
correctly sizes the dialog if given more than 2000 lines of text. [4314839]
display dialog
correctly displays “^0”, “^1”, “^2”, and “^3” in the text. [4831383]
A script may not tell a remote application to do shell script
. [4241641]
Script Editor accurately reports the position of errors in scripts that contain Asian characters. [3457168]
The Library window correctly handles applications with Asian names. [5080569]
disk
objects distinguish between disks with identical names. [4141496]
Setting the file type
and creator type
properties works correctly on Intel systems. [4788442]
Setting the focused
property of a UI element
works. [4756520]
UI element
objects have more informative names. [4886664]
The frontmost
property of the process
class will always be false for UI element applications such as System Events. This means that first process whose frontmost is true
will return the same application as path to frontmost application
. [4175274, 5100612]
The process
class will report the correct name for an AppleScript application bundle. [4381260]
Attempting to use GUI Scripting commands when Universal Access is off will produce a more informative error message. [4774412]
Database Events' performance has been improved. [4124666]
Folder Actions have been re-architected to improve their stability and performance. [3663310, 3693421, 4614337]
Folder Actions Setup allows attaching a script bundle as an action. [4036743]
Image Events handles files whose name contains “/” correctly. [4275156]
AppleScript can now read an application’s sdef directly. This means that an application with only an sdef no longer needs to be launched to get its scripting terminology. [4569425]
There are a few changes to the sdef format itself; see the sdef(5) man page for details. In particular, sdefs now support the use of XInclude. Applications that process sdefs should be prepared to handle them. The 10.4 sdef format is still supported, but you should migrate when you have the opportunity.
Several Open Scripting Architecture (OSA) APIs have new replacements, mostly intended for improved handling of Unicode script content. Except as noted, the new APIs are available in Mac OS X version 10.5 and later, and the old APIs are still available and supported, though they do not support the new functionality. For additional details, see the Open Scripting Architecture Reference.
old | new |
---|---|
These return their output as a | |
These use a CFArray of CFDictionaries of attributes to apply to the | |
|
OSALoadFile
and OSADoScriptFile
now correctly handle text scripts encoded as UTF-8. [4490939]
The various OSADebugger
APIs, which have been marked as “not implemented” for some time, have been completely removed. [3918369]
Scripting additions may be written using a new architecture which is both easier to write and improves performance. See Scripting Additions for Mac OS X for details. [4236732]
The scripting addition loader will search the directory specified by the environment variable DYLD_FRAMEWORK_PATH
. Xcode sets this variable to the build product directory when you run your executable, which means you can run and debug a scripting addition project directly from Xcode without first installing the scripting addition. [5027805]
© 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-12-11)
|