Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [64]

By Root 1379 0
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.

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 initialized—the 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 value—namely, 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 well—they 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.

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

Variable Names


The name of a variable must begin with a letter or underscore and must consist entirely of alphanumeric or underscore characters. So a variable name must begin with a character in the character set [a-zA-Z_] and must consist entirely of characters in the character set [a-zA-Z0-9_].

Case-Insensitivity of Variable Names


Variable names are case-insensitive at compile time. That means the following code will compile and run:

set myVar to 5

set myvar to myvar + 1

AppleScript assumes that myvar in the second line is the same variable as myVar in the first line. Furthermore, as a reflection of this assumption, AppleScript rewrites the variable names after compilation (that is, during decompilation) so that their case matches the first usage of the name:

set myVar to 5

set myVar to myVar + 1

This phenomenon suggests a trick that can help you spot mistakes: when you first define a variable, use an uppercase letter in its letter; elsewhere, never use an uppercase letter in a variable name. Then, after compilation, any variable name without an uppercase

Return Main Page Previous Page Next Page

®Online Book Reader