It can be a challenge trying to remember all of AppleScript's operators so here is a quick cheat sheet I use when scripting a comparison against strings, integers or boolean values. If you would like to follow along with any of the AppleScript samples I would encourage it. I'm using Script Editor on MacOS Sierra.
What is an operator?
Per Apple's documentation reference an operator is a symbol, word, or phrase that derives a value from another value or pair of values. AppleScript provides logical and mathematical operators, as well as operators for containment, concatenation, and obtaining a reference to an object. Operators that operate on two values are called binary operators, while operators that operate on a single value are known as unary operators.
The and & or operators
The and
and or
operators are two operators I commonly use that are great when evaluating a
boolean.
and & or sample
and operator
set pg1 to true set pg2 to false
if pg1 = true and pg2 = false then
return "programming"
else
return "monkeys"
end if
Result: programming
or operator
set pg1 to true
set pg2 to true
if pg1 = false or pg2 = true then
return "programming"
else
return "monkeys"
end if
Result:programming
Concatenation, Equality & Inequality operators
operator | symbol | description | other calls |
---|---|---|---|
Concatenation | `&` | A binary operator that joins two values. If the left-hand operand is a text object, the result is a text object (and only in this case does AppleScript try to coerce the value of the right-hand operand to match that of the left). | |
Equality | `=` | A binary comparison operator that results in true if both operands have the same value. The operands can be of any class. | is equal; equals; [is] equal to |
Inequality | `≠` | A binary comparison operator that results in true if its two operands have different values. The operands can be of any class. | is not; isn't; isn't equal [to]; doesn't equal; does not equal |
Concatenation, Equality & Inequality Sample
Concatenation operator
set pg1 to "The sky is blue."
set pg2 to "Shall we play outside?"
return pg1 & " " & pg2
Result:
The sky is blue. Shall we play outside?
Equality operator
set pg1 to 4
set pg2 to 3
if pg1 is equal to pg2 then
return "programming"
else
return "monkeys"
end if
Result: monkeys
Inequality operator
set pg1 to 4
set pg2 to 3
if pg1 is not equal to pg2 then
return "programming"
else
return "monkeys"
end if
Result:
programming
Greater than & less than operators
operator | symbol | description | other calls |
---|---|---|---|
Greater than | `>` | A binary comparison operator that results in true if the value of the left-hand operand is greater than the value of the right-hand operand | [is] greater than; comes after; is not less than or equal [to]; isn't less than or equal [to] |
Less than | `<` | A binary comparison operator that results in true if the value of the left-hand operand is less than the value of the right-hand operand | [is] less than; comes before; is not greater than or equal [to]; isn't greater than or equal [to] |
Greater than or equal to | `>=` | A binary comparison operator that results in true if the value of the left-hand operand is greater than or equal to the value of the right-hand operand. | [is] greater than or equal [to]; is not less than; isn't less than; does not come before; doesn't come before |
Less than or equal to | `<=` | A binary comparison operator that results in true if the value of the left-hand operand is less than or equal to the value of the right-hand operand. | [is] less than or equal [to]; is not greater than; isn't greater than; does not come after; doesn't come after |
Greater than & less than sample
Greater than
set pg1 to 4
set pg2 to 3
if pg1 > pg2 then
return "programming"
else
return "monkeys"
end if
Result:
programming
Greater than or equal to
set pg1 to 4
set pg2 to 3
if pg1 is greater than or equal to pg2 then
return "programming"
else
return "monkeys"
end if
Result:
programming
Less than
set pg1 to 4
set pg2 to 3
if pg1 < pg2 then
return "programming"
else
return "monkeys"
end if
Result:
monkeys
Less than or equal to
set pg1 to 4
set pg2 to 3
if pg1 is less than or equal to pg2 then
return "programming"
else
return "monkeys"
end if
Result:
monkeys