Chapter 3. Data TypesIn Chapter 1, I touched on data types, but now I will delve deeper! A data type describes how a programming system stores data in memory, and it is similar to the types used by other languages such as Java or Visual Basic. AppleScript data types specify the type of value that a variable stores (e.g., date, integer, string, real) or that an AppleScript command or scripting addition returns (see Appendix A). The data type that a variable stores determines what the script can do with it afterward, such as perform a math operation on an integer type or find out the length property (the number of characters) of a string type. This chapter only describes the built-in AppleScript data types; however, a variable could also store a reference to an object such as a file, a web URL, or a database record (see Chapter 1 and its discussion of Apple event objects). Table 3-1 lists the data types described in this chapter, which also includes the correct syntax to use when storing a certain data type in a variable and the other data types to which a variable can allowably be cast or coerced.
With some exceptions such as the date data type, you do not have to declare a data type when you declare a variable. When you declare a variable and store a string in it, such as: set theString to "I am a string" AppleScript will naturally enough store the literal value "I am a string" as a string. (The set keyword is used to store values in variables; this is summarized in Chapter 1 and explained in more detail in Chapter 6.) The same is true with boolean value types, such as: set theTruth to false AppleScript knows that theTruth is storing a boolean value. When you store a number with a fractional part in a variable, AppleScript automatically sets that variable to a real. There are several exceptions, however, to this loosely-typed nature. Consider Example 3-1 of a number and a string (number is just a synonym for a real or integer). When you run the example, you'll find that the number starts out as a data type integer, then takes part in a math operation that uses the / division operator. The results of operations that use this operator are always of type real.
Similarly, in Example 3-1, what starts out as a string (which otherwise looks like a date) is coerced to a very different date object. By "coerced" I mean the variable's data-storage method is altered to that of another data type. This is sometimes called "casting" from one data type to another in other programming languages such as Java. In AppleScript, there are some casts that are allowed (for example, from a number to a string) and others that are not allowed (a real to an integer if the real has a fractional part). The reference sections for each data type later in this chapter include an "allowed coercions" section, which describes the casts or coercions that are allowed for each data type. Example 3-1. An Example of Coercionset theNumber to 25 log class of theNumber -- theNumber is an integer type set theDate to "December 12, 1999" log class of theDate -- theDate is a string set theNumber to (theNumber / 3) (* theNumber is result of / operation so it's coerced to a real*) log class of theNumber -- class is now real set theDate to date theDate -- theDate string is coerced to a date value type log class of theDate In the second-to-last line, the theDate variable that stores a string is coerced to a date object. The class of theDate part of this example returns the class property of the date object, which is a nice way to look at which value type it is storing. The following sections describe each AppleScript data type in alphabetical order, including the other data types to which they can be coerced. |