Creating References to Local Variables

You can’t make a reference to a local variable. Well, you can, but you won’t be able to use it. For example:

local x
set x to {1, 2, 3}
set y to a reference to x
get item 1 of y -- error

This does not mean, however, that the only things you can create references to are top-level globals. You can make a reference to anything that isn’t a local, such as a property:

script myScript
        property x : 5
        set y to a reference to x
        set contents of y to x + 1
        display dialog x
end script
run myScript -- 6

Get AppleScript: The Definitive Guide 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.