Reference as Parameter
You can pass a reference as a parameter to a handler, and it remains a reference. The fact that the variable where the reference is stored may be a local is irrelevant; so is the fact that the parameter is local to the handler.
So, for example:
local x
tell application "Finder"
set x to folder 1
end tell
on setName(theRef)
set name of theRef to "Jack"
end setName
setName(x)In that code, x is a local, and
theRef is too (because a handler parameter is
local within the handler). But the code still works; it changes the
name of a folder in the Finder.
But you can’t pass a reference to a local, because
you can’t make a reference to a local. Well, you
can, but you can’t use it as a reference.
That’s why the reference to
operator can’t provide a
general solution to the problem of passing by reference (see Chapter 8).
So, for example:
global x
local y
set x to 5
set y to 5
on doubleRef(theRef)
set contents of theRef to 2 * theRef
end doubleRef
doubleRef(a reference to x)
display dialog x -- 10
doubleRef(a reference to y) -- errorThe difference between x and y
in this code is purely that x is global while
y is local. Applying the contents of operator to x works; applying it to
y causes an error.
The use of a reference as a parameter can permit a handler to perform dynamic targeting. As long as a handler doesn’t use any vocabulary that depends on a specific target, it can target an application whose identity is not known until runtime. In this example, the same code ...
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