AppleScript “repeat while” examples

AppleScript while loop FAQ: When using macOS, can you share some examples of the AppleScript while loop syntax (i.e., 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.

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

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

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

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

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