Creating a Reference

We've 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. ...

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.