AppleScript_ The Definitive Guide - Matt Neuburg [135]
However, when a coercion request is sent to an application and it refuses, if the datatype to be coerced is something AppleScript knows about, then AppleScript will also take a turn. This affects the error message that you'll see if the coercion fails:
tell application "Finder"
get folder 1 as integer -- error: Finder got an error: Unknown object type
end tell
The error in that example is attributed to the Finder. AppleScript doesn't know what a folder is so it doesn't get involved. But compare this:
tell application "Finder"
get name of folder 1 as integer -- error: Can't make "Mannie" into type integer
end tell
The error comes from AppleScript itself. The Finder was sent the coercion request as part of the get command, but it didn't obey that part; it returned a string (actually, Unicode text). AppleScript sees this, and attempts to perform the coercion itself—and fails.
An application can refuse to perform even the most elementary coercion:
tell application "Finder"
folder 1 as list -- error: Finder got an error: Unknown object type
end tell
That is an easy trap to fall into, because you are likely to become accustomed to turning a value into a one-item list easily with as list, so you're not expecting it to fail. This object, however, belongs to the Finder, so any attempt to coerce it to anything is going to be passed on to the Finder. Because the Finder won't perform this coercion, and because AppleScript itself doesn't know what a folder is, there is absolutely no way the coercion can be performed. Luckily there are other ways to achieve the same effect:
tell application "Finder"
{folder 1}
end tell
As I've said, you generally have no way of knowing in advance what coercions an application is willing to perform for you, or what the rules of those coercions may be. The application's dictionary doesn't tell you. You just have to find out by experimentation. The rest of this chapter is about AppleScript's native datatypes; what coercions can be performed on a nonnative type belonging to some application is anybody's guess.
Name
as
Synopsis
coercion
coercion
Syntax
value as class
Description
If value is a native datatype, you're asking AppleScript to coerce it to class. If this is a coercion AppleScript is willing to perform, the result is a new value of the requested datatype. If not, there's a runtime error.
Name
get... as
Synopsis
coercion by target
coercion by target
Syntax
[get]reference as class
Description
If reference is an object or attribute of some application, you're asking that application to fetch it and coerce it to class. If the application is willing, the result is a value of the requested datatype. If not, there's a runtime error.
Boolean Coercions
A boolean may be coerced to a string; depending on whether the boolean is true or false, this string will be either "true" or "false".
A string may be coerced to a boolean . The string "true" (not case-sensitive) will be true; any other string will be false.
A boolean may be coerced to an integer ; depending on whether the boolean is true or false, this integer will be either 1 or 0.
The integers 1 and 0 may be coerced to a boolean, yielding true and false respectively; other integers can't be coerced to a boolean.
Number, String, and Date Coercions
An integer may be coerced to a real .
A real (within in the integer range) may be coerced to an integer; it is rounded to the nearest integer. This was a new feature starting in Panther; in earlier versions of AppleScript, a real could be coerced to an integer only if it was an integer (that is, it had no fractional part). The old behavior is still present in repeat with (see Chapter 19) and, as you'll see in a moment, in coercion from a string (I regard this as a bug).
Warning
Observe that AppleScript's real-to-integer coercion rule is not like that of other computer languages you may know, such as in C, where the fractional part is thrown away. To throw away the fractional part of x, say x div 1 (see Chapter 15). The round