Why do these even exist? It seems absurd. Like with most dynamic languages, AppleScript types seem to be either immutable primitive types like integer
s and real
s which are going to be handed around by value and don't make any sense to use with a reference to
, or object-like types like application
s, script
s, or record
s, which are being passed around by reference already. How is a reference to
not completely redundant? Here's an example taken from Apple's AppleScript Language Guide (https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/introduction/ASLR_intro.html):
tell app "Finder" to set diskRef to a ref to startup disk
--result: startup disk of application "Finder"
So do you mean to tell me that if I did this instead,
tell app "Finder" to set diskObj to startup disk
--result: startup disk of application "Finder"
that the applescript runtime is going to send an apple event sent across to the Finder process telling it, "hey - some guy just asked you to return an octet stream of /dev/disks01 back to me! Haha! I guess he should have asked for a reference to
it! Let's get started! This is going to take a while!"
I'm programming in Python and I do this:
m = fileHandle.read( 1000000000 ) #and then wait a little while
n = m
Did I just copy a gig of data around in memory? Of course not. But the existence of a reference to
in AppleScript implies that assigning objects to new variables is a by-value operation. And if that's the case, what's the deal with the copy
command?
What's going on here?
UPDATE: Well, just consider me a confused Python programmer. Just to make this a bit more clear, I still think
tell app "Finder" to set diskRef to a ref to startup disk
--result: startup disk of application "Finder"
is a poor example (taken from the applescript language guide). But @Chuck's example of a reference to
the property itself holding a primitive type that can then be reassigned is a better one. IOW, a reference
object is really a variable/property that holds a pointer to another variable or property.
My understanding is that you can think of references as pointers.
set x = 5
set y to reference to x
set contents of y to 10
log x -- 10
In general, you don't manually create references. AppleScript libraries and dictionaries may return them, but then you work with the properties of the returned item (name of startup disk
for example).
Honestly, I'd ignore them. In twenty years of working with AppleScript, I've probably had to look up the documentation to references once. If you're trying to do something in AppleScript and believe you need to create a reference variable, you're probably not doing it in the most straightforward manner.
Check out this MacTech article for a more detailed discussion of AppleScript references.