Variable Declaration

You can use either the set or copy keywords to declare a variable and assign a value to it, as in the following examples:

set myvar to (5 * 25)
copy (5 * 25) to myvar

Both of these statements produce the same result; they store the integer 125 in the myvar variable. The set version however is more intuitive and is used more often to declare variables. set and copy furthermore have different results when you use the variable to contain a list, record, or script object. (Chapter 9, discusses this AppleScript feature.) The copy keyword, as in:

copy listVar to newListVar

will make a new copy of the list value stored in listVar and store this new list in newListVar. If you used:

set newListVar to listVar

the list stored in newListVar will still refer to the original list (i.e., listVar). The newListVar variable will not get a new copy of the list when you use set. Chapter 6 goes further into this set and copy subject.

Get AppleScript in a Nutshell 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.