Skip to content

For Loops in AppleScript: Repeat

This article on MacTech summarizes the loop flow control structures in AppleScript which apparently amounts to the “repeat” command.

repeat
   -- code
end repeat

this will repeat indefinitely but you can exit the repeat within such a block by adding something like this:

if stopMe = true then
   exit repeat
end if

You can also repeat a fixed number of times:

repeat 5 times
   -- code
end repeat

You can also repeat until some condition:

repeat while keepGoing = true
   -- code
end repeat
repeat until stopMe = true
   -- code
end repeat

Finally there is a kind of repeat which is familiar to anyone who has used increments in for loops in other programming languages:

repeat with x from 1 to 10
   -- code which can refer to the number x which increases with each repeat
end repeat

2 Comments

  1. PeterVk wrote:

    Simple, clean advice. Thanks.

    Really nice site, mate!

    Saturday, June 21, 2008 at 9:03 pm | Permalink
  2. Alex wrote:

    There’s also the “repeat with «item» in «list»” form.

    Wednesday, May 25, 2011 at 2:29 am | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*

Creative Commons Attribution 3.0 Unported
This work is licensed under a Creative Commons Attribution 3.0 Unported.