Language Guide

CHAPTER 6
Expressions
Variables 155
To avoid data sharing for lists, records, and script objects, use the Copy
command instead of the Set command. The Copy command makes a
copy of the list, record, or script object. Changing the value of the original
changes does not change the value of the variable. Here’s an example of
using Copy instead of Set to create the variable yourList.
set myList to { 1, 2, 3 }
copy myList to yourList --this command makes a copy of
--mylist
set item 1 of myList to 4
get yourList --result: { 1, 2, 3 }
If you update myList, the value of yourList is still {1, 2, 3}.
Scope of Variables 6
The scope of a variable determines where else in a script you may refer to the
same variable. The scope of a variable in turn depends on where you declare it
and whether you declare it as global or local.
After you define a global variable in a script, you can make subsequent
references to the same variable either at the top level of the script or in any
of the script’s subroutines. After you define a local variable, you can make
subsequent references to the same variable only at the same level of the script
at which you defined the variable.
AppleScript assumes that all variables defined at the top level of a script or
within its subroutines are local unless you explicitly declare them as global. For
more detailed information and examples of the use of variables in subroutines,
see “Recursive Subroutines,” which begins on page 225.
You can also declare variables within script objects. The scope of variables in a
script object is limited to that script object. For more information, see “Scope of
Script Variables and Properties,” which begins on page 252.