Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [63]

By Root 1391 0
y, z} to {1, 2, 3}

z -- 3, and can you guess what x and y are?

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.

Name

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.

Name

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.

Declaration and Definition of Variables


There is no requirement in AppleScript that variables be declared explicitly. The rule is basically that if you use a word that AppleScript doesn't understand, the word is assumed to be the name of a variable. The following code, as a complete script, will compile just fine:

set x to x + 1

Although it is not necessary to declare a variable, it is possible to declare a variable. I'll explain how you do this, and argue that there are good reasons for doing it, in Chapter 10.

Definition


The code in that last example, as a complete script, will compile, but it won't run; at runtime, it generates an error. The error message reads: "The variable x is not defined." The problem is not that the variable x has never been declared! There is no need to declare it. AppleScript understands (or assumes) that x is supposed to be a variable. Nor is the problem that you are trying to assign to it. The problem is that you are trying to fetch its value, and it has no value. That's because x has never been assigned a value. An AppleScript variable is not defined until you first explicitly give it a value. To continue our shoebox analogy, there is no "x" shoebox to fetch the contents of, because you've never put anything into it.

This code both compiles and runs:

set x to 5

set x to x + 1

During execution of the first line, AppleScript observes that you're putting something into the "x" shoebox, but there is no such shoebox as yet. No problem; AppleScript creates the shoebox, labels it "x", and puts 5 into it. Now the second line runs fine, because there is a shoebox "x" from which to fetch a value.

Once a variable has been defined, it generally stays defined until its scope finishes executing. There is no command explicitly letting you "undefine " a variable or assign the "undefined" value to it. There are a couple of constants, such as null and missing value, that you can assign as a sort of placeholder to signify that a variable hasn't been assigned any other value (see Chapter 17). However, you can in fact genuinely undefine a variable by assigning to it the result of a command that has no result. This is typically an accident: you were expecting a command to return a value, but it doesn't. Code

Return Main Page Previous Page Next Page

®Online Book Reader