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 ...

Get AppleScript: The Definitive Guide, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.