Relation Operators for Strings
|1||begins with / starts with|
|3||is equal to|
These operators can be negated too. Like for example, does not contain, does not begin with, is not in, etc.
if true then -- insert actions here end if if false then -- insert actions here end if
Explanation: If the operation performed returns true then it will execute certain set commands.
If the operation performed returns false then it will execute certain set commands. The if command ends with end if.
set myAge to 20 if myAge is greater than or equal to 18 then beep display dialog "You are eligible to Vote" say "You are eligible to Vote" using "Zarvox" end if
Explanation: This program is intended to check whether you are eligible to vote or not. Here I created a variable named myAge. It stores my age.
Then to check my eligibility, I created a if condition. But in order to execute the commands inside the if condition, my comparison should be true that is myAge>=18. If comparison is true, the a beep sound will be made and a dialog will be displayed saying you are eligible to vote. Finally Zarvox will say that you eligible to vote.
Figure 10.2.2 if Condition
set myAge to 17 if myAge is greater than or equal to 18 then beep display dialog "You are eligible to Vote" say "You are eligible to Vote" using "Zarvox" else display dialog "You are not eligible to Vote" end if
Explanation: This program is intended to check whether you are eligible to vote or not. However I have added an else condition too. If the age comparison is false then script will execute the else section.
Figure 10.2.3 if...else Condition
set myName to "Nayan Seth" if myName is equal to "Nayan Seth" then display dialog "True" end if -- Starts With if myName starts with "Nayan" then beep end if
Explanation: In the above Script we are comparing strings and then using if conditions. So I have created a variable named myName. It stores my name.
In the first if condition I am checking if myName is equal to “Nayan Seth”. Since the comparison is true, a dialog will display True.
In second if condition, I am checking if myName begins with “Nayan”. As this comparison holds true too. So a beep sound will be made.
Figure 10.2.4 if Condition for Strings
if "Nayan" comes before "Seth" then beep end if
Explanation: comes before or comes after works alphabetically. Here I am checking if Nayan comes before Seth. As “N” (first letter of Nayan) comes before “S” (first letter of Seth), the condition holds true and a beep sound will be made.
set singleCharacter to "s" set myName to "Nayan Seth" considering case if myName contains singleCharacter then beep else display dialog "Does not contain " & singleCharacter end if end considering
Explanation: It may so happen that you want to make case sensitive comparisons. It’s simple, use considering case command.
In the above example I am checking if singleCharacter is present in myName. Do note singleCharacter is “s” and myName contains “S”. So condition holds false and so the else section will be executed that is a dialog will be displayed.
Figure 10.2.6 Case Sensitive
set fullName to "Naya n Seth" set myName to "Nayan Seth" ignoring white space if myName is equal to fullName then display dialog "Success" else beep end if end ignoring
Explanation: What if you want to compare strings but ignore white space (it means space between two characters)? Then you can make use of ignoring white space command.
Here I have two strings fullName & myName that contain “Naya n Seth” and “Nayan Seth” respectively. I have ignored white space and I am checking if they are equal. So the comparison will be done on “NayanSeth” and “NayanSeth”. Since both are equal, if condition will be executed that is displaying a dialog.
Figure 10.2.7 Ignoring White Space