AppleScript_ The Definitive Guide - Matt Neuburg [134]
You should not, however, count on such behavior. For one thing, you have absolutely no way of knowing when it will work; a scriptable application's dictionary doesn't tell you (see "Coercions" in Chapter 20). The Finder's dictionary here says "integer," not "I'd like an integer, but if you're fool enough to send me a string, I suppose I'll coerce it for you." For another thing, it doesn't always work. For example, suppose you say this:
tell application "Finder"
set name of folder 1 to 6
-- error: Finder got an error: Can't make some data into the expected type
end tell
Again, as you can see, AppleScript does not help out; it just sends the Finder what you said to send it. It is then up to the Finder to decide whether it's happy with what was sent. In this case, the Finder is not happy, and lets you know with an error.
Explicit Coercion
Explicit coercion is performed with the as operator. There are actually two cases, depending on whether the value to be coerced is a native AppleScript datatype and belongs to your script. These two cases amount to two different operators, even though AppleScript (in its usual misguided attempt to make things "easy") makes them look the same.
Reference Section
Reference Section
Coercion by AppleScript
Coercion by AppleScript is the subject of this chapter, which tells you what coercions AppleScript is willing to perform. For example:
9 as string -- "9"
9 as boolean -- error: Can't make 9 into type boolean
Even though a variable's value can be a class, you can't use a variable as the second operand in a coercion. This won't even compile:
set className to string
9 as className -- compile-time error: Expected class name but found identifier
AppleScript must see a class name right there in the script at compile time, or it won't parse the line. (I regard this as a bug.)
Do not confuse a coercion with an object string specifier! (See "Object String Specifier" in Chapter 11.) This is a coercion:
"feathers:" as alias
This is an object string specifier:
alias
"feathers:"
The distinction can be crucial. There are circumstances where the coercion will compile but the object specifier will not. You can't compile an alias specifier that uses a literal string unless the file exists, but you can compile a coercion from any string to an alias. And there are circumstances where the object string specifier will compile but the coercion will not. You can form a file specifier using a pathname string, but you can't coerce a string to a file object . (See "File Coercions," later in this chapter.)
Coercion by a Scriptable Application
Here's an example of an application being asked to perform a coercion:
tell application "Finder"
folder 1 as string
end tell
That does not involve a coercion performed by AppleScript. AppleScript can't coerce a folder to a string. It doesn't even know what a folder is. It is a coercion request targeted entirely at the Finder. It happens that the Finder is happy to comply—it returns the pathname of the folder.
How do you know when a coercion will be performed by AppleScript and when it will be performed by a scriptable application? It depends on what is to be coerced. This code is in a tell block, but no message is sent to the Finder; AppleScript performs the coercion:
tell application "Finder"
get "9" as integer
end tell
The value "9" is a complete target. One might equally have said:
tell application "Finder"
tell "9"
get it as integer
end tell
end tell
The presence of the Finder in a tell block surrounding this code is ignored. At the other extreme:
tell application "Finder"
set x to folder 1
end tell
name of x as integer
In that code, x is a reference to an object in the Finder, so obtaining name of x involves sending a message to the Finder. That message is modified by as; the Finder is asked to perform the coercion. All of this makes sense in terms of who the target is, as explained in Chapters 11 and 12.