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. 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.
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 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:
|
|||||||||||||||||||||||||||||||||
Table of contents |