Previous Book Contents Book Index Next

Inside Macintosh: AppleScript Language Guide / Part 2 - AppleScript Language Reference
Chapter 4 - Commands / Command Definitions

Set AppleScript command, application command

The Set command can function as an AppleScript command or an application command. The AppleScript command assigns one or more values to one
or more variables. It can also be used to share data among lists, records, or script objects (see "Notes"). The application command sets the values of one
or more objects.

set variablePattern to expression
expression returning variablePattern
set referencePattern to expression
expression returning referencePattern 
The name of the variable in which to store the value, or a list of variable patterns, or a record of variable patterns.
Class: Identifier, list, or record
The expression whose value or values are to be assigned.
If expression is a reference or a list or record of references, AppleScript gets the values of the objects specified by
the references.
Class: For a variable, any class.
A reference to the location whose value is to be set, or a list of reference patterns, or a record of reference patterns.
Class: Reference, list, or record
The value assigned.

You can use the Set command to set a variable to any value:

set x to 5
set myList to { 1, 2, "four" }
tell application "Scriptable Text Editor"
   set x to word 1 of front document
end tell
These two statements are equivalent:

set x to 3
3 returning x
Similarly, the following examples are equivalent:

tell front document of application "Scriptable Text Editor"
   set x to word 1
end tell
tell front document of application "Scriptable Text Editor"
   word 1 returning x
end tell
In addition to setting a variable to a single value, you can set patterns of variables to patterns of values. For example, this script sets a list of two variables to the position of the front window.

tell application "Scriptable Text Editor"
   set {x, y} to position of front window
end tell
Since the Scriptable Text Editor returns position of front window as a list of two integers, the preceding example sets x to the first item in the list and y to the second item.

Patterns set with the Set command can also be more complex. Here are
some examples:

set x to {8, 94133, {firstName:"John", lastName:"Chapman"}}
set {p, q, r} to x
(* now p, q, and r have these values: 
            p = 8
            q = 94133
            r = {firstName:"John", lastName:"Chapman"} *)
tell front document of application "Scriptable Text Editor"
   set {word 1, word 2} to �
      {firstName of item 3 of x, lastName of item 3 of x}
end tell
--now word 1 = "John" and word 2 = "Chapman"
set {p, q, {lastName:r}} to x
(* now p, q, and r have these values: p = 8
                                      q = 94133
                                      r = "Chapman" *)
As the last example demonstrates, the properties of a record need not be given in the same order and need not all be used when you set a pattern to a pattern, as long as the patterns match.

The use of the Set command with patterns is similar to the use of patterned parameters with subroutines, which is described in "Subroutines With Positional Parameters," beginning on page 235.

If you use the Set command to set a variable to a list, record, or script object, the variable shares data with the original list, record, or script object. If you change the data of the original, the value of the variable also changes. Here's an example of how this works:

set myList to { 1, 2, 3 }
set yourList to myList
set item 1 of myList to 4
The result of these statements is that item 1 of both myList and yourList is 4.

Data sharing promotes efficiency when using large data structures. Rather than making copies of shared data, the same data can belong to multiple structures. When one structure is updated, the others are automatically updated.

To avoid data sharing for lists, records, and script objects, use the Copy command instead of the Set command.
Only data in lists, records, and script objects can be shared; you cannot share other values. Moreover, you can share data only on the same computer, and the shared structures must all be in the same script.

Previous Book Contents Book Index Next

© Apple Computer, Inc.
13 JUL 1996