3.6. Compound Relational Tests
You might need to make more sophisticated decisions in your programs based on more than one comparison. For example, at the beginning of the chapter, you said you would go to the Yankees' game if it wasn't raining. Suppose you said that the temperature also had to be above 60° F. In other words, you're going to the ballgame only if both conditions are met: It's not raining and the temperature is above 60° F. Using what you've learned so far, you could express this in AppleScript notation by writing a statement like this:
if it's not raining then if the temperature's above 60 degrees then display dialog "Take me out to the ballgame!" end if end if
An easier way to ask both of these questions at the same time is by using the and operator. You can use this operator to test if two Boolean expressions are both true; if they are, the result is true. If either is not true (that is, false), the result is false.
If you were to use this operator to rewrite the previous conditions, it would look like this:
if it's raining and the temp's above 60 then display dialog "Take me out to the ballgame!" end if
The or operator tests to see if either of two Boolean expressions is true, and if so, the result is true.
The not operator is used to negate a Boolean expression, making a true value false, and a false value true.
These three Boolean operators are summarized in the following table.
Boolean Operator | Meaning | Example |
---|---|---|
boolean1 and boolean2 | true if both expressions are ... |
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.