Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [116]

By Root 1422 0
seen that a reference might be handed to you by some application, but you can also create one yourself. To do so, you use the a reference to operator. (An abbreviation is ref, which is a lot easier to type. Intermediate abbreviation forms like a ref and ref to are legal as well.) For example:

set x to 100

set y to a reference to x

When you create a reference, the phrase you use is effectively what gets frozen into the reference as an incantation. Thus, in a situation where you would have been handed a reference anyway, asking explicitly for a reference could get you a different reference:

tell application "Finder"

set x to a reference to folder 1

end tell

x -- folder 1 of application "Finder"

What you say is what you get. And what you say doesn't have to exist, either—it doesn't even have to make sense! As long as the compiler can resolve the terminology, it will compile your phrase. The fact that it's unusable doesn't matter; you're not using it, you're just freezing it for later. Thus no error arises, no matter how silly your phrase may be. Of course, later on if you do try to use it, you'll find out if it's a valid thing to say at that point:

tell application "Finder"

set x to a reference

to disk 99 of folder 1 of label "yoho"

end tell

get name of x --error: Can't get name of disk 99 of folder 1 of label "yoho"

What that shows is that asking explicitly for a reference does not send an Apple event; it merely creates the Apple event so you can save it for later.

Identifying References


AppleScript goes to some lengths to hide the existence of references, making it remarkably difficult to find out that a value is a reference. Properly speaking, a reference should be a class, a datatype like string or integer (see "Class" in Chapter 11, and Chapter 13). If you ask a string about its class, it says string. If you ask an integer about its class, it says integer. But if you ask a reference about its class, it will never tell the truth and say reference.

set x to a reference to "hey"

set y to a reference to 9

tell application "Finder" to set z to folder 1

class of x -- string

class of y -- integer

class of z --folder

Here are a couple of tricks you can use to learn that a value is a reference. (I don't guarantee any of them, but they do seem mostly to work.)

The reference coercion trick

The only thing that can be coerced to a reference is a reference. If you try to coerce anything else to a reference, you'll get a runtime error. So try to coerce a value to a reference, and if there's no error, it is a reference. For example:

tell application "Finder" to set x to folder 1

x as reference -- no error; it's a reference

The editor value trick

If a value, as shown in your script editor application, contains the word of, it is a reference. For example:

tell application "Finder" to set x to folder 1

x -- folder "Mannie" of...; it's a reference

set x to a reference to y

x --y of «script»; it's a reference

When I'm debugging or developing a script, I like the second method best; I look at a variable's value and I can usually see right away whether it's likely to be a reference. If I'm writing code where the code itself needs to test whether something is a reference, I like the first method best. Here's a general handler that returns a boolean value telling whether its parameter is a reference:

on isRef(valueToTestAsRef)

try

valueToTestAsRef as reference

return true

on error

return false

end try

end isRef

-- and here's how to call it

tell application "Finder"

set x to folder 1

end tell

isRef(x) -- true

set x to "haha"

isRef(x) -- false

isRef(a reference to x)) --true

Dereferencing a Reference


Once you have a variable whose value is a reference, AppleScript behaves with confusing inconsistency when you try to use it. In some cases, you can't use the reference unless you explicitly dereference it; in other cases, AppleScript dereferences it for you implicitly when you use it. AppleScript can even behave both ways with one and the same reference.

When AppleScript

Return Main Page Previous Page Next Page

®Online Book Reader