Previous Page
Next Page

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

as

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.

get... as

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.

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

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

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


Previous Page
Next Page