Operators and Reference Forms

An operator is a symbol or token that is used with values or variables in an AppleScript expression. An example is the well-worn expression 2 + 2 = 4 (if you just dropped this expression into a Script Editor window, it would return a boolean value of true). The operators in this expression are “+” and “=”. AppleScript has most of the operators that you would expect a scripting language to make available to the programmer. AppleScript also allows the scripter to use very readable English expressions for operators, such as:

if 5 is greater than 3 and 6 equals 6 then set bool to true

The principal symbolic operators are demonstrated in Example 1-10. All operators, including the English forms, are described in Chapter 4.

Example 1-10. AppleScript Operators
(* & concatenates one string to another, or combines two or more lists or 
records *)
set twoPhrases to "One phrase " & "connected to another phrase."

(* the following code returns {"a string inside of a list", "added at the end 
of a sentence."} *)
set twoLists to {"a string inside of a list"} & {"added at the end of a 
sentence."}
(* & also combines two records to make one record. *)
set twoRecs to {firstn:"Amanda"} & {secondn:"Smith"}

(* parentheses and Math operators do what you would expect them to *)
set int to (5 * 6) - 8 -- returns 22

(*If you use / or ÷ the result is always a real data type. If you use 
div the 
result is always an integer *)
set n1 to 50 / 26  -- returns 1.923076923077
set n2 to 50 ÷ 26  -- returns 1.923076923077
set n3 to 50 div 26  -- returns 1; div only returns integer data types

(* < ≤ > ≥ ≠ = are used to test equality *)
set bool to 50 < 26 -- bool is false
set bool to 50 > 26 -- bool is true
set bool to 50 = 26 -- bool is false
set bool to 50 ≠ 26 -- bool is true

(* ^ is the exponentiation operator *)
set n1 to 50 ^ 2 -- n1 is 2500.0, a real data type

(* mod returns the fractional part and throws out the rest of the integer 
part, the opposite of div which throws out the fractional part *)
set n2 to 7 mod 3 -- n2 is 1

(* not, or, and are boolean operators; they are used to combine two expressions
to produce a boolean result *)
set bool to true and false -- bool is false
set bool to true or false -- bool is true
set bool to not true -- bool is false
set bool to (2 + 2 = 4) and (not (2 ^ 2 = 4)) (* you can combine expressions
to get a result; bool is false in this case because the second part of the 
expression (i.e., (not (2 ^ 2 = 4)) ) evaluates to false *)

A reference form is an English or symbolic expression that describes where a value is within its container. As an AppleScripter, you will often find yourself describing contained objects in order to accomplish your task, such as “get the first record in the database named myDB” or, “get the second paragraph of the last document in the folder named January Stuff.” AppleScript offers numerous ways to refer to these contained items. Chapter 4 goes into great detail in describing these methods, which are demonstrated in short by Example 1-11. For example, you can describe contained items by referring to the first-tenth item, then with anything that requires a reference that exceeds 10 you use the number, as in: get the 1000th word of document "Mydocument".

Example 1-11. Different Ways to Refer to Contained Items
tell application "Finder" to get the folder after (the 20th folder of startup 
disk)
(* gets the folder object after the 20th folder on the startup disk *)
tell application "Finder" to get the folder after the 20th folder of¬ 
startup disk
tell application "Finder" to get the folder before the 20th folder of¬
startup disk

You can create very useful and detailed scripts using AppleScript’s numerous reference forms. Example 1-12 gets only one part of a list of numbers (i.e., the numbers 3 through 6) and stores this sub-section in another list variable. The following section briefly describes the repeat and if...end if statements that appear here.

Example 1-12. Using the Range Reference Form
set list1 to {1, 2, 3, 4, 5, 6}
set list2 to (numbers 3 thru 6 in list1) -- returns only {3,4,5,6}