Basics of AppleScript

if...else

Relation Operators for Strings

Serial No AppleScript
1 begins with / starts with
2 ends with
3 is equal to
4 comes before
5 is in
6 contains

These operators can be negated too. Like for example, does not contain, does not begin with, is not in, etc.

Script [10.2.1]:

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.

Script [10.2.2]:

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

Figure 10.2.2 if Condition

Script [10.2.3]:

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

Figure 10.2.3 if...else Condition

Script [10.2.4]:

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

Figure 10.2.4 if Condition for Strings

Script [10.2.5]:

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.

Script [10.2.6]:

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

Figure 10.2.6 Case Sensitive

Script [10.2.7]:

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

Figure 10.2.7 Ignoring White Space