Team LiB   Previous Section   Next Section
boolean

Allowed coercions

list with one item, as in {true}
string

Syntax

set theTruth to true

Description

boolean data types can only be one of two values: true or false. When setting a variable to a literal boolean value, just type true or false without quotation marks. Expressions that include comparison operators return boolean values (see the following Examples section).

Two of the examples in the "Examples" section demonstrate that AppleScript does not consider case by default when comparing string values. If you enclose the string comparison in a considering case...end considering block, however, the case of the characters (upper- or lowercase) matters. Therefore, the last expression in "Examples" returns false because of the uppercase "I" in the first operand and the lowercase "i" in the second operand.

Three of the examples use three logical operators: and, or, and not. These are AppleScript language elements that you will use all the time to test or alter boolean values. For example, the following phrase finds all files that are not gif files:

get (every file in folder "images" whose not (name ends with "gif"))

If you have to, you can set the value of a boolean variable to the return value of an expression:

set theBool to ("I am" is equal to "i am")

The theBool variable evaluates to true. The parentheses are there to make it more readable; they are not necessary in this case.

You cannot use numerical values (such as 0, 1, or -1) as boolean values, unlike Perl. If you try to write

if (-1) then display dialog "hey!"

then you will get a pithy error message on the order of "can't make -1 into a boolean value." In Perl, lots of different values evaluate to true. Any number is true except 0, including 1 and -1; string values other than the "empty string" ("") evaluate to true. Not so with AppleScript—boolean values are either true, false, or a boolean return value from a comparison expression, AppleScript command, or application-property value.

Examples

This expression returns true, because 35 is greater than 25:

35 > 25

This expression also returns true because the math is correct:

25 is less than 35

This expression returns false because the not operator reverses the value of true:

not true

With and, both operands must evaluate to true for the expression to be true:

true and false -- returns false

This expression returns true because the first operand is true and the expression "short circuits" (i.e., returns true without evaluating the second operand):

true or false -- returns true

AppleScript doesn't consider case by default and finds two strings to be equal:

"I am" is equal to "i am" -- returns true

This expression returns a false value because case sensitivity is taken into account:

considering case ... "I am" is equal to "i am" ...end considering 

(* returns false *)
    Team LiB   Previous Section   Next Section