[ACCEPTED]-Applescript equivalent of "continue"?-continue
After searching for this exact problem, I 16 found this book extract online. It exactly answers the 15 question of how to skip the current iteration 14 and jump straight to the next iteration 13 of a repeat
loop.
Applescript has exit repeat
, which will 12 completely end a loop, skipping all remaining 11 iterations. This can be useful in an infinite 10 loop, but isn't what we want in this case.
Apparently 9 a continue
-like feature does not exist in AppleScript, but 8 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 7 display the dialogs for 1, 2, 4 and 5.
Here, you've 6 created two loops: the outer loop is your 5 actual loop, the inner loop is a loop that 4 repeats only once. The exit repeat
will exit the inner 3 loop, continuing with the outer loop: exactly 2 what we want!
Obviously, if you use this, you 1 will lose the ability to do a normal exit repeat
.
set aList to {"1", "2", "3", "4", "5"}
repeat with anItem in aList -- # actual loop
try
set value to item 1 of anItem
if value = "3" then error 0 -- # simulated `continue`
log value
end try
end repeat
This will still give you the "exit repeat" possibillity
0
set aList to {"1", "2", "3", "4", "5"}
repeat with anItem in aList -- # actual loop
try -- # needed to simulate continue
set value to item 1 of anItem
if value = "3" then continueRepeat -- # simulated `continue` throws an error to exit the try block
log value
on error e
if e does not contain "continueRepeat" then error e -- # Keeps error throwing intact
end try
end repeat
Based on the try block based approach above 6 but reads slightly better. Of course, since 5 continueRepeat is not defined an error will 4 be thrown which causes the rest of the try 3 block to be skipped.
To keep error throwing 2 intact include the on error clause that 1 throws any unexpected error.
-- Or you could use different strategy: use 2 the loop to loop, and do the conditional 1 logic in a handler, like so:
set aList to {"1", "2", "3", "4", "5"}
repeat with anItem in aList
doConditionalWork(anItem as string)
end repeat
on doConditionalWork(value)
if value = "3" then return
display dialog value
end doConditionalWork
Y'all are all overcomplicating it. Try this:
set aList to {"1", "2", "3", "4", "5"}
repeat with anItem in aList
set value to item 1 of anItem
if value is not "3" then log value
end repeat
0
You can also use “repeat while” for loops 1 that only repeat conditionally.
Based on the answer of Tom Lokhorst, here is a 14 variant to do either break
or continue
, based on a isExit
variable 13 you set.
For that at the end of the outer 12 (non-fake) loop, you add
if isExit then
exit repeat
end if
That way if isExit
is 11 true, you simply exit the outer loop too, being 10 an effective break
.
Original question/answer:
For the original question 9 problem this would look like that, below 8 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 7 down, into an more easy example:
Say you'd 6 want to have either continue
or break
in those two if
s.
You 5 can place all your code between those, I 4 only included the code relevant for the 3 different exit strategies.
If you want to 2 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 1 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
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.