Control statements

    Normally, AppleScript executes statements one after the other. Control statements can change that order. You can use a control statement to skip statements or repeat statements.

    Control statements can contain other control statements, so you can develop complex sequences in your script based on changing conditions.

    Following is a brief description of some of the control statements you can use in your scripts.

Tell statements

    The Tell statement specifies the default object to which commands are sent.

    tell front window of application "Finder" close

    If you don't specify the default object, AppleScript sends the command to the current application.

    As with other control statements, you can use Tell statements within a Tell statement.

If statements

    The If statement shown in the following example lets you control which statements are executed based on a condition. An If statement contains at least one expression that will be true or false when the script is run.

    Show me an If statement.

    If the expression following the word "if" is true, then the statement following "then" is executed. If the expression is false, then the statement is not executed.

    You can add an Else clause to the If statement to cause a statement to be executed if the condition is false. The Else clause must be the last part of the If statement, as in the next example.

    Show me an If statement with an Else clause.

    You can use Else If clauses prior to the final Else clause to include more conditions and statements. You can also nest If statements within an If statement.

Try statements

    Commands can result in error messages. The Try statement lets you control what happens when an error occurs. A Try statement includes statements to execute if an error occurs. Here's the basic form:

    try
    [ statements ]
    on error
    [ statements ]
    end try

    If an error occurs while executing the statements following Try, the statement after the On Error clause are executed.

Repeat statements

    The Repeat statement lets you execute statements repeatedly. It can include a variable to control the number of times the statements are executed.

    The Repeat statement has several forms:
    Repeat (indefinitely): Repeats the statements indefinitely.
    repeat
    [ statements ]
    end repeat
    Use the Exit statement in an If statement to stop repeating the statements.
    Repeat Number Times: Repeats the statements a specified number of times.
    repeat integer [ times ]
    [ statements ]
    end repeat
    Repeat While: Repeats the statements while a condition is true.
    repeat while expression is true
    [ statements ]
    end repeat
    Repeat Until: Repeats the statements until a condition is true.
    repeat until expression is true
    [ statements ]
    end repeat
    Repeat With Variable: Repeats the statements for a number of times through a range of values.
    repeat with loopVariable from startValue¬
    to stopValue [by stepValue]
    [ statements ]
    end repeat
    StartValue and stopValue determine the range of the variable. StepValue determines the size of steps through the range. The default is 1.
    Repeat With Variable In List: Repeats the statements using a list to define the range.
    repeat with loopVariable in list
    [ statements ]
    end repeat
    Exit: Stop a repeat statement.

 


Table of contents