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

3 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
  3. lindenthal wrote:

    excellent list

    I had a look for something like this:

    repeat until exists sheet 1 of window 1 but no more than 20 times

    like this:
    repeat 20 times
    delay 0.1
    if exists sheet 1 of window 1 then
    exit repeat
    end if
    end repeat

    Friday, February 8, 2013 at 5:36 am | Permalink