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 without explicitly dereferencing the variable; in other cases, AppleScript dereferences it for you implicitly when you use it. AppleScript can behave both ways with one and the same reference.
When AppleScript performs implicit dereferencing, the reference is completely transparent: it acts precisely as if you were saying the incantation that’s frozen inside it. This is exactly the same phenomenon noted in the previous section—you can’t learn from a reference that it is a reference, because it acts as if it were the thing referred to.
tell application "Finder"
set x to folder 1
end tell
name of x -- Mannie
class of x -- folder
set name of x to "Moe"None of that ought to be possible. A reference’s
class isn’t folder, and a
reference doesn’t have a name
property that you can get and set. In this case, though, it happens
that the reference is a reference to a thing
whose class is folder and that has a
name property. AppleScript dereferences the
reference implicitly; it treats the reference as if it were the thing
referred to.
But in this example, an attempt to use the same reference transparently runs up against a brick wall:
tell application "Finder"
set x to a reference to the name of folder 1
end tell
set x to "Moe"If you were hoping that this would set the name of the
Finder’s folder 1 to ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access