Previous Book Contents Book Index Next

Inside Macintosh: AppleScript Language Guide / Part 2 - AppleScript Language Reference
Chapter 6 - Expressions / Variables

Data Sharing

Data sharing allows you to create two or more variables that share the same list, record, or script object data; it can be used to promote efficiency when working with large data structures. Only data in lists, records, and script objects can be shared; you cannot share other values. In addition, the shared structures must all be on the same computer.

To create a variable that shares data with another variable whose value is a list, record, or script object, use the Set command. For example, the second Set command in the following example creates the variable yourList, which shares data with the previously defined variable myList.

set myList to { 1, 2, 3 }
set yourList to myList  --this command creates yourList,
                        --which shares data with myList
set item 1 of myList to 4
get yourList --result:{ 4, 2, 3}
If you update myList by setting the value of its first item to 4, then the value of both myList and yourList is {4, 2, 3}. Rather than having multiple copies of shared data, the same data belongs to multiple structures. When one structure is updated, the other is automatically updated.

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
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}.

Previous Book Contents Book Index Next

© Apple Computer, Inc.
13 JUL 1996