DekGenius.com
[ Team LiB ] Previous Section Next Section

7.2 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

7.2.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. That's because x has never been assigned a value. 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. An AppleScript variable is not defined until you first give it a value explicitly. 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 in the course of running a script, it generally stays defined until its scope finishes executing, as discussed later in the chapter. There is no command explicitly letting you "undefine" a variable or assign the "undefined" value to it. However, you can 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 Section 8.1.

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

7.2.2 Initialization

A variable is initialized (given its first value) when you explicitly assign it its first value. There is no auto-initialization of variables in AppleScript, and there is no special syntax for initializing variables. A variable is undefined until you assign it a value; at that moment it is defined and initialized—the variable now exists, it has a value, and it is possible to fetch that value.

The exception is a script property. A script property is a kind of global variable, and it is declared and initialized in the same line of code. Script properties have some other interesting features, which are discussed later in this chapter.

7.2.3 Typing

A variable in AppleScript has no fixed type. By this I mean simply that it is permissible to assign any variable any value, 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 (Chapter 13, Chapter 14, and Chapter 15).

7.2.4 Explicit Declaration

Although it is not necessary to declare a variable, it is possible to declare a variable, and there are three ways to do so:

  • As a script property:

    property x : 5
    set x to x + 1
  • As a global:

    global x
    set x to 5
    set x to x + 1
  • As a local:

    local x
    set x to 5
    set x to x + 1

The meanings of these declarations are explained later in this chapter.

It is almost always best to declare your variables. Your code will be easier to understand, and the scoping rules are simpler and clearer, if all your variables are declared. Unfortunately there is no way to have AppleScript to warn you when a variable is not declared.


    [ Team LiB ] Previous Section Next Section