Applescript equivalent of "continue"?

Viewed 25913

I have a simple 'repeat with' in an AppleScript, and would like to move on to the next item in the "repeat" conditionally. Basically I'm looking for something similar to "continue" (or break?) in other languages.

I'm not well versed in AppleScript but I have found it useful a few times now.

7 Answers

After searching for this exact problem, I found this book extract online. It exactly answers the question of how to skip the current iteration and jump straight to the next iteration of a repeat loop.

Applescript has exit repeat, which will completely end a loop, skipping all remaining iterations. This can be useful in an infinite loop, but isn't what we want in this case.

Apparently a continue-like feature does not exist in AppleScript, but here is a trick to simulate it:

set aList to {"1", "2", "3", "4", "5"}

repeat with anItem in aList -- # actual loop
    repeat 1 times -- # fake loop
        set value to item 1 of anItem

        if value = "3" then exit repeat -- # simulated `continue`

        display dialog value
    end repeat
end repeat

This will display the dialogs for 1, 2, 4 and 5.

Here, you've created two loops: the outer loop is your actual loop, the inner loop is a loop that repeats only once. The exit repeat will exit the inner loop, continuing with the outer loop: exactly what we want!

Obviously, if you use this, you will lose the ability to do a normal exit repeat.

Based on the answer of Tom Lokhorst, here is a variant to do either break or continue, based on a isExit variable you set. For that at the end of the outer (non-fake) loop, you add

if isExit then
    exit repeat
end if

That way if isExit is true, you simply exit the outer loop too, being an effective break.

Original question/answer:

For the original question problem this would look like that, below will be a generalized one.

set aList to {"1", "2", "3", "4", "5"}

repeat with anItem in aList -- actual loop
    set isExit to false
    repeat 1 times -- fake loop
        -- do your stuff
        set value to item 1 of anItem

        if value = "3" then 
            -- simulated `continue`
            set isExit to false
            exit repeat
        end if

        if value = "69" then 
        -- simulated `break`
            set isExit to true
            exit repeat
        end if
        
        display dialog value
    end repeat

    if isExit then
        exit repeat
    end if
end repeat

Generalized

So to break this down, into an more easy example:

Say you'd want to have either continue or break in those two ifs. You can place all your code between those, I only included the code relevant for the different exit strategies.

If you want to write something like this:

repeat <condition_repeat>
    if <condition_continue> then
        continue
    end if
    if <condition_break> then
        break
    end if
end repeat

That could be written as

repeat <condition_repeat>
    set isExit to false                -- added 
    repeat 1 times                     -- added 
        if <condition_continue> then
            set isExit to false        -- changed
            exit repeat                -- changed
        end if
        if <condition_break> then
            set isExit to true         -- changed
            exit repeat                -- changed
        end if
    end repeat                         -- added
    if isExit then                     -- added
        exit repeat                    -- added
    end if                             -- added
end repeat
Related