10.5 Changes
This article describes changes to AppleScript and related tools in Mac OS X Leopard v10.5 and its updates.
Unicode Support
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 anid
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"
returns65
, andcharacter 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}
, andstring id {104, 101, 108, 108, 111}
returns"hello"
. (Because of a bug,text id ...
does not work; you must use one ofstring
,Unicode text
, orcharacter
.) These obsolete the olderASCII character
andASCII 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 grapheme cluster as a single character.Some “characters” may be composed of a series of Unicode code points, what Unicode defines as a grapheme cluster. For example, “é” may be encoded as U+0065 (LATIN SMALL LETTER E), U+0301 (COMBINING ACUTE ACCENT). AppleScript 2.0 will count the cluster as one character, where older versions counted the base character and combining mark separately. For a complete definition of grapheme clusters, see UAX #29: Unicode Text Segmentation. [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]
Compatibility
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
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 arunning
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 afrontmost
property similar torunning
, including that the information is available from System Events, but it tells you whether or not the application is in front. The value offrontmost
for background-only applications, UI element applications such as System Events, and applications that are not running is alwaysfalse
.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-inname
property; the value is usable as the name of an application totell
.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"
, andapplication id "emal"
all refer to Mail.app, and will all work as the target of atell
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
oractivate
.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 intell
blocks, are evaluated every time the script runs. This alleviates problems with scripts getting “stuck” to a particular copy of an application. [4356296]
Compatibility
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.
Scriptability and VoiceOver
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.
Scriptable Network Preferences
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 |
Command Line Support
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.
Compatibility
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.
Other Enhancements
Script Editor
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]
System Events
The
disk
class hasserver
andzone
properties for use with AppleShare volumes. [3554247]The
file
class has adefault application
property, corresponding toinfo for
'sdefault application
property and Finder'sopen 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 abundle identifier
property. [4782866]The
process
class has anarchitecture
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'sname
orid
property, respectively.XML element named ...
andXML element id ...
will also work. [4437103, 4441834]
Image Events
Image Events supports RAW images. [4250666]
The
pad
command has an optionalwith pad color
parameter. [5234464]The
save
command has an optionalwith compression level
parameter to specify the JPEG compression level. [3614780]
Bug Fixes
AppleScript
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
anddate
specifiers, in particularalias "..."
andPOSIX 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]
Standard Additions
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
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]
System Events
disk
objects distinguish between disks with identical names. [4141496]Setting the
file type
andcreator type
properties works correctly on Intel systems. [4788442]Setting the
focused
property of aUI element
works. [4756520]UI element
objects have more informative names. [4886664]The
frontmost
property of theprocess
class will always be false for UI element applications such as System Events. This means thatfirst process whose frontmost is true
will return the same application aspath 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]
Others
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]
Developer Notes
Scripting Definitions (sdefs)
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.
API changes
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
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]
Copyright © 2017 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2017-09-19