AppleScript's repeat loop

Posted in AppleScript on October 04, 2016 · 1 min read
How can I use AppleScript repeat loop in Script Editor?

AppleScript's repeat loop is a controlled statement which is defined as a statement that determines how and when other statements execute or evaluate. When coding for a repeat make sure to remember that AppleScript has reserved the words repeat and end like:

repeat theLoop
end repeat

so if you trying using these for something like setting a variable you will likely get an error when compiling. There are numerous types of repeat statements with each differing in their termination such as a numerical repeat, forever repeat and repeat with a condition.

An example of a numerical repeat loop:

set theCount to 5

repeat 5 times
   set theCount to theCount + 1
end repeat

return theCount

Result: 10

An example of a forever repeat loop:

set theCount to 10

repeat
   set theCount to theCount + 1
end repeat

return theCount

which if run by itself will produce an infinite loop that can only be stopped by clicking the

Stop the Script button or the combination of cmd+.. The best usage of an infinite repeat is with a conditional statement known as a termination condition. To end the repeat with a conditional you could code it as:
set theCount to 10

repeat
   set theCount to theCount + 1
   if theCount is 15 then exit repeat
end repeat

return theCount

Result: 15

or with a return like:

set theCount to 10

repeat
   set theCount to theCount + 1

   if theCount is 15 then return theCount
end repeat

return theCount

Result: 15

If you wanted to evaluate a list you could code it as:

## create the list
set theList to {"option 1", "option 2", "option 3"}

## create the loop to step into the list
repeat with x from 1 to (length of theList)

   ## setting each list item to a variable
   set evaluateList to item x of theList

   ## stepping into the list with a conditional
   if {"option 2"} is in evaluateList then
      display dialog "found"
   end if
end repeat

Result: you get a dialog box of found

One idea of a good script that could be used to pass to others with a repeat loop is to detect if something exists in a string of text that you do not want.

We'll say that you cannot use any text with the word banana as all the monkeys would go into chaos wanting a banana so we forbid even speaking or writing it. Instead of getting tons of emails asking if the string of text is ok we can send them a script so they can check for themselves:

with timeout of (10 * 60) seconds ## good to timeout a repeat

    ## we request the text
    set theRequest to text returned of ¬
        (display dialog "Please enter your string" default answer null)

    ## set each word to a variable for stepping
    set theWords to words in theRequest

    ## step into each word
    repeat with badBanana in theWords

        ## conditional to evaluate if it's there
        if contents of badBanana is equal to "banana" then

            ## display if it's found
            display dialog "YOU USED BANANA!!"

        end if

    end repeat

end timeout

Result: YOU USED BANANA!!
Help the site go ad free:
Storm Trooper