Previous Page
Next Page

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

7.3.1. 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 for doing it on purpose appears under "Returned Value" in Chapter 9.

There is no way to ask whether a variable's value is defined; all you can do is fetch its value, and if it's undefined you'll get an error. It would then be up to your code to handle this error (see "Errors" in Chapter 19); otherwise your script will simply stop running at that point.

7.3.2. Initialization

A variable is initialized (given its first value) when you explicitly assign it its first value. Even when you explicitly declare a variable, there is no autoinitialization in AppleScript. A variable is undefined until you assign it a value; at that moment it is defined and initializedthe variable now exists, it has a value, and it is possible to fetch that value.

In general, there is no special syntax for initializing variables. There are, however, three exceptions: a script object definition, a handler definition, and the definition of something called a script property (it's a kind of global variable; I'll explain further in "Script Properties" in Chapter 8). In all three cases, a variable is defined and initialized by a special syntax not involving set or copy. For example, consider the definition of this script object called s:

script s
    display dialog "howdy"
end script

When you say that, you're actually defining a variable called s and initializing it with a valuenamely, the script object itself. A handler definition works just the same way. This may seem an odd way to think of such entities, but I assure you that it is strictly true and is part of what makes AppleScript cool: a script object and a handler are actually variable values just like any other value (such as 5 or "howdy"), and you can do with them, and with the variables that name them, all sorts of stuff that you can do with any other values and variables. It's no coincidence, either, that script object definitions, handler definitions, and script properties have a special syntax of definition and initialization, as they have other things in common as wellthey are the three top-level entities of a script object, and their scopes work in parallel ways. We'll return to all these matters in detail later.

7.3.3. Typing

A variable in AppleScript has no fixed type. By this I mean simply that it is permissible to assign any value to any variable at any time. The following code is legal:

set x to 5
set x to 5.2
set x to "hello"
set x to string
set x to {"fee", "fie", "fo", "fum"}
set x to (path to current user folder)

In that code, x becomes successively an integer, a real, a string, a class , a list, and an alias. A defined variable (one that has a value) has a type, called its class; this is simply the class (datatype ) of its current value, and it changes if a value of a different class is assigned to it.

The various built-in datatypes, and the ways in which AppleScript lets you coerce implicitly and explicitly from one to another, are discussed later in this book (Chapters 13, 14, and 15).


Previous Page
Next Page