Basics of AppleScript

Logical Conditions

Logical conditions that we will look at will be AND & OR. This section reminds me of Electronics and first year of Engineering.

Script [10.5.1]:

set x to true
set y to true
if x and y then
    display dialog "True"
else
    display dialog "False"
end if

Explanation: For AND to hold true, both inputs should be true. In above Script both x and y are true and hence if section will get executed.

Figure 10.5.1

Figure 10.5.1 AND Condition

Script [10.5.2]:

set x to true
set y to false
if x or y then
    display dialog "True"
else
    display dialog "False"
end if

Explanation: For OR to hold true, either one of the inputs should be true. In above Script, x is true whereas y is fals. However the if condition is met, hence if section will get executed.

Figure 10.5.2

Figure 10.5.2 OR Condition

Script [10.5.3]:

set x to true
set y to false
set z to (x and y) -- z is false
set p to (x or y) -- p is true

Explanation: Here x is true and y is false. I declared variable z and assigned it with value of AND operation performed on x and y. p is assigned with value of OR operation performed on x and y. Hence z is false and p is true.

Script [10.5.4]:

set x to true
set y to "xyz"
if x and y = "xyz" then
    beep
else
    say "False Condition"
end if

Explanation: Here x is a boolean type and y is String type. In if condition I have used AND command where one of the variable is true. However we need to check for y variable. So I am checking contents of y variable with xyz. If they match then the other variable will return true. And since both return true the if section will get executed.