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 ...
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