Flylib.com

Books Software

 
 
 

Section 6.5. Variables


6.5. Variables

To complete our map of the world of a script, we must include variables. A variable is an association ( formally , a binding ) between a name and a value. You can think of it as a shoebox with a label on it, into which something is placed for storage. The shoebox's label is the variable's name ; what's inside the shoebox is the variable's value. For example, when we say:

set x to 5

it is as if we had a shoebox labeled "x" into which we place the number 5.

Naturally, variables are very useful things, which is why just about all computer languages have them. To be able to assign your own names to things makes your program clearer and easier to maintain. And in a way any computer program is all about manipulating values, so it's nice to have a place to put each value when you're not using it, so that you can retrieve it again later. A variable's value is like the coffee in your cup when you set the cup on the table, do something else, and then pick up the cup and take a sip. The cup is like the variable; without it, the coffee would just flow onto the floor when you're not using it.

Variables and code are intimately related . Code is where variables are defined; code is where variables are given their values and where those values are retrieved.

Variables and scope blocks (handler definitions and script object definitions) are intimately related. Variables are what scope is all about: they are the "things" that other regions of the code either can or can't see, depending on the rules of scope, the location of the code, and the nature of the walls constructed by the scope blocks. In fact, we can go even further and say that every variable "lives" at the top level of a handler or a script object (and those are the only places where a variable can "live"). In fact, we can go even further than that: handlers and script objects live at the top level of a script object too, and this is because handlers and script objects are themselves variable values. This may sound confusing right now, but after you've read a few more chapters, it will all seem perfectly natural.

Script objects, handlers, variables, and code constitute the entire structure of a script. There is nothing else. Together they are AppleScript's "gang of four," the sole and supreme rulers of the AppleScript world. We have now mapped that world completely. Let's review. At the top is the script object; the script itself is a script object. At the top level of a script object there can be variables, handlers, and script objects. At the top level of a handler there can be variables and script objects, but a handler cannot be nested directly in a handler. Handlers and script objects are the scope blocks, determining where their variables can be seen. Code can go only in a handler, but that handler might be a script object's implicit run handler, so in that sense code can go in a script object too.



Chapter 7. Variables

This chapter describes the rules for assignment , declaration, typing, initialization, and naming of variables in the AppleScript language.



7.2. Assignment and Retrieval

If a variable is a labelled shoebox (see "Variables" in Chapter 6), then to assign a value to a variable is to put something into the shoebox. If the variable already has a value, that value is replaced .

Assignment is performed with one of two commands: set or copy .

set

Syntax

set


variableName


to


value



Description

Assigns value to variableName .

Example

set x to 5

There is a synonym using the word returning instead of set , with the parameters in reverse order, like this: 5 returning x . But I have never seen this synonym used.

copy

Syntax

copy


value


to


variableName



Description

Assigns value to variableName .

Example

copy 5 to x

An abbreviation for copy is put ; an abbreviation for to is into . Thus you could type put 5 into x although it would still come out as copy 5 to x . These abbreviations were designed to accommodate HyperCard users, who were habituated to this idiom.

There is no simple assignment operator, such as the equals sign ( = ). You cannot, for example, perform an assignment like this:

x = 5

That is a comparison, and returns a boolean result revealing whether x is already 5. That code is legal (and therefore does not cause a compile-time error) but is not an assignment (as any mildly experienced programmer would expect); this is a frequent cause of bugs in my scripts. See "The "English-likeness" Monster" in Chapter 4.

7.2.1. Set by Reference

As they both perform assignment, you might think set and copy must be completely interchangeable. In most cases, they are; but with regard to four types of valuelists, records, dates, and script objectsthey are not. With these data types, set sets by reference , meaning that you can end up with more than one name for the same value.

The reason why these four data types are singled out for special treatment is that they are the only kinds of value that can be mutated in place. Thus, after a set by reference, whatever mutation is performed upon such a value under one of its names applies to it under its other names as well. For example:

set L to {1, 2, 3}
set LL to L --

set by reference

set end of L to 4 --

mutate a list in place

LL --

{1, 2, 3, 4}


, because it is the same list as L


For other datatypes, use whichever command you prefer; I habitually use set .

7.2.2. Multiple Assignment

In an assignment, variableName and value can optionally be listsof variable names and values, respectivelyallowing multiple assignments in one command. The first item in the value list is assigned to the first item in the variableName list, the second to the second, and so forth. If the value list is longer than the variableName list, the extra values are not assigned to anything; if the value list is shorter than the variableName list, there is a runtime error. This remarkably elegant feature is probably underutilized by beginners . (For a parallel construction involving assignment to a record, see "Record" in Chapter 13.) For example:

set {x, y, z} to {1, 2, 3}
z --

3


, and can you guess what

x

and

y

are?


7.2.3. Retrieval

To retrieve the value of a variable (or fetch the value, or use the value, or whatever you want to call it), simply use the variable's name in code. As with most computer languages, there is no problem retrieving from and assigning to the same variable in a single statement:

set x to x + 1

There is no circularity, because the value of x is first retrieved, and 1 is added to that value; then the result of this addition is assigned to x , replacing the original value.

The result of a line consisting of just the name of a variable is the value of that variable. So, for example:

set x to 5
x

The result of that script is 5. This can be useful when you want to see the implicit result of a script for testing or debugging (see "Implicit Result" in Chapter 5).

It is possible to retrieve a variable's value by using the get command:

set x to (get x) + 1

But no one ever talks this way in real life, and as far as I know this use of get with a variable adds nothing. However, get with an object reference is another matter; see "Get" in Chapter 11.