Reference as Incantation

One very productive way to think of a reference is as an incantation . It's like frozen speech. It encapsulates a bit of phraseology, a particular utterance. If a variable's value is a reference, that value is something you can use to access the object it refers to. What I mean by "use" is "say": a reference is an encapsulation of the words you would have to utter (in your code, of course) in order to access the object. In a way, a reference is like a miniature package of suspended evaluation; it's a little phrase that isn't evaluated until you use it. When you do use it, it works just as if you'd said the phrase at that point in your code.

For example, consider this code:

tell application "Finder"
    set x to (get folder 1)
    display dialog (get name of x) -- Mannie
end tell

The dialog shows the name of the folder. Why does this work? As we have said, x is this reference:

folder "Mannie" of desktop of application "Finder"

This means that using x is like using those words. Therefore, when you say this:

get name of x

it's just like saying this:

get name of folder "Mannie" of desktop of application "Finder"

A reference answers the "What would I have to say?" question. What would I have to say in order to speak of the Finder's folder 1? The Finder tells us one answer; I could say this:

folder "Mannie" of desktop of application "Finder"

You may be disconcerted at first by that fact that this is not what you did say. You said folder 1, referring to the folder by index number; ...

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.