3.2. AppleScript's Relational Operators

A little earlier, I showed you how to test whether the value of a variable x was less than zero. You can do so by using the English phrase: is less than. This phrase is actually an operator in AppleScript and is more formally known as a relational operator. You use a relational operator to compare two expressions, one to the left of the operator and the other to the right. If the relation is valid, the result of the Boolean expression is true; otherwise, the relation does not hold and the result is false. When used in an if statement, the result of the comparison is tested to determine whether the statements that follow are to be executed.

As you might expect, AppleScript provides an assortment of relational operators. These operators exist in English word forms, but they also have their symbolic counterparts. For example, you can use the less than symbol < in place of the words and write an if statement that looks like this:

if x < 0 then set x to -x

The relational operators built-in to AppleScript are summarized in the following table. These operators can be used to compares numbers, strings, and dates (the equality and inequality operators can also be used to compare other data types like lists and records).

Rational OperatorSymbolExamples
is less than

is not greater than or equal to

comes before
<x is less than 0

x < 0

"a" comes before "b"
is less than or equal to

is not greater than

does not come after
(x+5) is less than or equal to

Get Beginning AppleScript® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.