AppleScript “repeat while” examples

AppleScript while loop FAQ: Can you share some examples of the AppleScript while loop syntax (the AppleScript repeat while syntax)?

Instead of using a keyword like while to create loops, AppleScript uses the repeat keyword to create loops. There are several versions of this syntax, as follows.

Back to top

AppleScript repeat syntax: Repeat X times

This example shows how to repeat an AppleScript command X number of times:

repeat 3 times
  say "Hello"
end repeat
Back to top

AppleScript “repeat while” syntax

Here’s a slightly different version of that AppleScript loop, using the repeat while syntax:

set i to 3
repeat while i > 0
  say "Hello"
  set i to i - 1
end repeat
Back to top

AppleScript “repeat until” syntax

And here’s the same example using the AppleScript repeat until syntax:

set i to 3
repeat until i = 0
  say "Hello"
  set i to i - 1
end repeat
Back to top

AppleScript “repeat with” syntax: Repeat with A from Y to Z

Here’s a little different repeat with syntax:

repeat with i from 1 to 3
  say "Hello"
end repeat
Back to top

AppleScript “repeat with” list example

To wrap it up, here’s one more example of the AppleScript “repeat” syntax, first using numbers in a list:

repeat with i in {1, 2, 3}
  say "Hello"
end repeat

and then using strings in a list:

repeat with i in {"Hello", "I", "must", "be", "going"}
  say i
end repeat
Back to top

Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.