Team LiB   Previous Section   Next Section

Chapter 7. Flow-Control Statements

The flow-control statements in AppleScript orchestrate the "flow" or the order in which the code statements execute in your scripts. Programmers will be familiar with AppleScript's if conditional statements, which are very similar to the syntax of Visual Basic, Perl, and other languages. These statements execute code only if the tested conditions are true. AppleScript handles loops in script code with several variations of the repeat statement, similar to the "for," "foreach," or "for each" statements in other languages. The repeat flow-control construct repeats the execution of code a specified number of times or for each member of a container, such as a list type. Or, it repeats a code phrase a specified number of times:

repeat 100 times...end repeat

You will be pleased to know that AppleScript has more than adequate error-trapping capabilities. This is accomplished by enclosing the statements that may raise errors in a try...end try statement block. In addition, you have already seen dozens of examples of the tell..end tell statement in earlier chapters. These statements specify the objects, usually application objects, that receive the commands or Apple events that your script sends. You specify the targets of different script commands by using these tell statements.

You can nest flow-control statements within other flow-control statements. Most of these statements end, appropriately, with the reserved word end, optionally followed by the statement identifier, such as tell or repeat. An example is:

tell app "Photoshop 5.5"...end tell

The if and tell statements allow "simple" rather than "compound" usage, such as:

if (current date) > date "1/1/2001" then display dialog "Welcome to 2001"

These simple statements appear on one line, they do not contain other code statements, and they do not need to be completed with the end reserved word. This code shows some nested flow-control statements and simple statements:

tell application "Finder"

   set freeMemoryBlock to largest free block

   (* Here's a simple statement; no 'end' is necessary *)

   if freeMemoryBlock < 10000000 then display dialog¬

   "Memory is getting low" 

   set listOfProcesses to name of processes

   if "BBEdit 5.0" is not in listOfProcesses then (* compound 'if' 

statement *)

      tell application "BBEdit 5.0" to run -- simple 'tell' statement

   end if

end tell

Suffice it to say, flow-control statements are how AppleScript derives much of its power and complexity. You will develop very few scripts that do not use at least one flow-control statement. Table 7-1 lists the statements that this chapter describes.

Table 7-1. Flow-Control Statements
considering
repeat with loop variable
continue
repeat integer times
error
return
exit
tell simple statement
if simple statement
tell compound statement
if compound statement
try
ignoring
using terms from
repeat
with timeout
repeat until
with transaction
repeat while
 

    Team LiB   Previous Section   Next Section