DekGenius.com
Team LiB   Previous Section   Next Section
if simple statement

Syntax

If theBool is true then exit repeat

Description

AppleScript supports the simple if statement that is similar to Perl's. You can use a statement such as the following:

if (current date) is greater than or equal to date "1/1/2001" then¬ display dialog "Welcome to 2001"

You do not have to "close" this statement with an end or end if, as you do with more wordy compound statements. You just include the reserved word if followed by a boolean expression (returns true or false), the reserved word then, and whichever statement you would like to execute if the boolean expression returns true. This example has several different versions of the if statement. Since there are two different versions of the same date-test expression, this script will create two of the same dialog boxes:

if (current date) is greater than or equal to date "Saturday, January¬
1,  2000 12:00:00 AM" then display dialog "Welcome to 2000" (* simple if statement *)
if (current date) is less than date "Saturday, January 1, 2000 12:00:00¬ 
AM" then display dialog "Enjoy end of 1999" -- simple if statement
   set yearCount to 0
   if (current date) 3 date "Saturday, January 1, 2000 12:00:00 AM" then 
   --compound if statement
   display dialog "Welcome to 2000"
   set yearCount to yearCount + 1
else if (current date)¬
 < date "Saturday, January 1, 2000 12:00:00 AM" then
   display dialog "Enjoy end of 1999"
end if

Use a simple if statement if the script only has to execute one line of code in the event that the boolean expression tests true. Otherwise use a compound if statement.

    Team LiB   Previous Section   Next Section