DekGenius.com
[ Team LiB ] Previous Section Next Section

14.2 Explicit Coercion

Explicit coercion is performed with the as operator.

ascoercion

Syntax

value as class

Description

When you use the as operator, you're asking AppleScript to perform the coercion. If this is a coercion AppleScript is willing to perform (as described in the rest of this chapter), the result is a new value of the requested datatype. If not, there's a runtime error.

Examples

9 as string -- "9"
9 as boolean -- error

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

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 Section 10.9.) This is a coercion:

"xxx:" as alias

This is an object string specifier:

alias "xxx:"

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 unless the file exists; but you can compile a coercion from a string to an alias, because the string is just a string. 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 anything, not even a pathname string, to a file object.

A special case of coercion arises when you say as in the context of a get, implicit or explicit, targeted at an application. When you do this, you are not using the as operator. You are using the get...as command, which is a very different thing. The as operator asks AppleScript to perform the coercion. The get...as command asks the target to return a particular class of value; in effect, you're asking the target to perform the coercion.

get...ascoercion by target

Syntax

[get] attribute as class

Example

tell application "Finder"
        folder 1 as string
end tell

An application may be willing to perform coercions of which AppleScript itself would be incapable. The preceding example demonstrates this. AppleScript itself can't coerce a folder to a string. It doesn't even know what a folder is. This is a request targeted entirely at the Finder, and it says: "Get folder 1, but please return a string as the result." It happens that the Finder has a way to do this, so it complies with this request—it returns the pathname of the folder.

You can be using the get...as command without realizing it if you become confused about who the target is. For example:

tell application "Finder"
        set f to folder 1
end tell
f as string

That looks like it shouldn't work. Having retrieved folder 1, we are no longer talking to the Finder. So when we try to coerce this folder to a string, we will be talking to AppleScript, and AppleScript won't know how to do it. Right? Wrong. The variable f isn't just a folder; it's a reference to a folder, a folder belonging to the Finder. (If this isn't clear to you, you should reread Chapter 11.)

tell application "Finder"
        set f to folder 1
end tell
f -- folder "myFolder" of desktop of application "Finder"

Therefore it's a complete and valid target. So when you say:

f as string

you're actually saying:

tell application "Finder"
        get (folder "myFolder" of desktop) as string
end tell

The target is the Finder and the get...as command is sent to the Finder.

If you ask an application to perform a coercion it isn't willing to perform, you'll receive the compulsory mysterious error message from the application:

tell application "Finder"
        set f to folder 1 as folder -- error: "Unknown object type."
end tell

Again, you usually 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.

    [ Team LiB ] Previous Section Next Section