Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [152]

By Root 1457 0
= "Enough!"

set response to button returned of ¬

(display dialog "Knock knock!" buttons {"Enough!", "Who's there?"})

end repeat

Tip

Those accustomed to the do...until construct in C and similar languages should observe that it is possible for an AppleScript repeat until block not to be executed even once.

Repeat With


The syntax of a repeat with announcement line is as follows:

repeat with variableName from startInteger to endInteger [by stepInteger]

Here's how a repeat with works:

When the repeat with announcement line is encountered for the first time, startInteger and endInteger (and stepInteger, if supplied) are evaluated once and for all. Each must be an integer (or a real or string representing an integer); if not, there's a runtime error.

If startInteger is larger than endInteger (or smaller if stepInteger is negative), that's the end of the loop and execution resumes after the end repeat line.

The value startInteger is assigned to the variable variableName, which is created as a local if not in scope already. (Note that variableName, if declared implicitly at the top level, will not be a global; this contradicts "Scope of Undeclared Variables" in Chapter 10.)

The block is executed. (If code within the block terminates the repetition prematurely, that's the end.)

The value 1 (or stepInteger if supplied) is added to the value that variableName was assigned before the previous repetition. If the resulting value is larger than endInteger (or smaller if stepInteger is negative), that's the end of the loop and execution resumes after the end repeat line. Otherwise, variableName is assigned this new value, and it's back to step 4 for another execution of the block.

If you read the description carefully, you will realize that:

There's no extra overhead involved if any of the integers in the repeat with line are derived from handler calls or commands, as the evaluation is performed only once. This behavior is in contrast to repeat while and repeat until.

After a repeat with is all over, the variable variableName has the value it had when the last repetition terminated.

Setting the variable variableName within a repeat with block affects the code that executes subsequently within the block, but it has no effect on the test performed at the top of the next repetition or on what value variableName will take on as the next repetition begins.

Here's a simple example of repeat with in action:

repeat with x from 3 to 1 by -1

display dialog x

end repeat

display dialog "Blast off!"

Repeat With... In


The syntax of a repeat with...in announcement line is as follows:

repeat with variableName inlist

This construct is a convenient way of cycling through each item of a list. Here's how to visualize the way it works:

The variable variableName is created as a local if it is not already in scope.

The size of the list is obtained. Call this number theCount.

A reference to the list is obtained. Call this theListRef.

A counter is initialized to zero. Call it x.

Before each repetition, x is incremented. If x exceeds theCount, that's the end of the loop, and execution resumes after the end repeat line. Otherwise, variableName is assigned this value:

a reference to item x of theListRef

The block is executed.

(In this sequence of steps, none of the variable names are real: there is nothing called theCount, theListRef, or x. But AppleScript is truly maintaining these values internally; I gave them names simply for clarity.)

The big surprise here is step 5, which sets the value of variableName. At the start of each repetition, nothing is copied from the list into variableName. It's a reference (see Chapter 12). So, for example, in this loop:

repeat with x in {1, 2, 3}

-- code

end repeat

the variable x does not take on the values 1, 2, 3. It takes on these successive values:

a reference to item 1 of {1, 2, 3}

a reference to item 2 of {1, 2, 3}

a reference to item 3 of {1, 2, 3}

References are often transparently dereferenced,

Return Main Page Previous Page Next Page

®Online Book Reader