More on Conditionals |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| comparison | can be used on |
| > is greater than comes after |
number, string |
| < is less than comes before |
number, string |
| >= &Mac179; (option-period) is greater than or equal to |
number, string |
| <= &Mac178; (option-comma) is less than or equal to |
number, string |
| = is equal to is |
all types |
| <> &Mac173; (option-equals) is not equal to is not |
all types |
| contains | string, list, record |
| does not contain | string, list, record |
| is in | string, list, record |
| is not in | string, list, record |
| starts with | string, list |
| ends with | string, list |
| (Note: some special characters may not display properly in your browser. They may be typed in Script Editor using the keystrokes noted in parentheses.) | |
True to AppleScript's plain-english nature, many of these comparison operators can be typed differently--you can type equals or does not equal or is contained by and it will still work, but the compiled text will read is equal to, is not equal to, or is in instead of what you typed. Also, be careful when using the last six operators on lists and records--for example:
| example | result | explanation |
| "MacWarriors" contains "Mac" | true | The string "Mac" is a subset of the string "MacWarriors". |
| {"MacWarriors", "AppleScript"} contains "Mac" | false | "Mac" isn't an item of the list. |
| {"MacWarriors", "AppleScript"} starts with "MacWarriors" | true | "MacWarriors" is the first item of the list. |
| {name:"Steve", title:"iCEO"} contains "iCEO" | false | "iCEO" isn't an element of the record. |
| {name:"Steve", title:"iCEO"} contains {title:"iCEO"} | true | {title:"iCEO"} is a valid field/value pair in the record. |
Of course, string comparisons can get messy: what if your script is looking for the user to type "AppleScript" and they instead type "applescript", or if you're looking for "café" and they type "cafe"? AppleScript has a set of built-in rules for this, which can be changed by using the considering or ignoring control structure. Here are the default rules:
| term | example | default |
| case | "MacWarriors" vs. "macwarriors" | ignored |
| white space | "PowerMac" vs. "Power Mac" | considered |
| diacriticals | "café" vs. "cafe" | considered |
| hyphens | "okie-dokie" vs. "okiedokie" | considered |
| punctuation | "cool!" vs. "cool" | considered |
The white space term includes collections of returns, spaces, or tabs anywhere within a string, and punctuation includes most any character that's not included in the other terms. Here are some examples of the considering/ignoring block, all of which will beep given a variable stringThing whose contents are "AppleScript is cool!":
considering case
if stringThing contains "AppleScript" then beep
end considering
ignoring white space
if stringThing contains "Apple Script" then beep
end ignoring
considering case but ignoring punctuation
if stringThing ends with "cool" then beep
end considering