Monday, May 25, 2020

#1 2020-05-20 01:01:25 am

olli
Member
Registered: 2008-11-01
Posts: 30

How to exit a repeat with loop after 10 iteration regardless

I have an Applescript with contains the following snippet:

Applescript:


--
-- theElements variable is initialised here
--
repeat with theElement in theElements
--
-- do lots of stuff with theElement
--
end repeat

Unfortunately without adding anything the loop might run for many iterations - for test purposes how can I exit the loop after say 10 or 100 iterations?


Filed under: loop, Counter, repeat-with

Offline

 

#2 2020-05-20 02:03:20 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5202

Re: How to exit a repeat with loop after 10 iteration regardless

Hi.

If you don't know how many elements there'll be (ie. more or less than your limit), the best way would be to implement a counter:

Applescript:

set limit to 10
set counter to 0

repeat with theElement in theElements
   set counter to counter + 1
   if (counter > limit) then exit repeat
   --
   -- do lots of stuff with theElement
   --    
end repeat

Or:

Applescript:

set limit to 10

repeat with counter from 1 to (count theElements)
   if (counter > limit) then exit repeat
   set theElement to item counter of theElements
   --
   -- do lots of stuff with theElement
   --    
end repeat

Last edited by Nigel Garvey (2020-05-20 02:21:48 am)


NG

Offline

 

#3 2020-05-20 06:40:19 am

olli
Member
Registered: 2008-11-01
Posts: 30

Re: How to exit a repeat with loop after 10 iteration regardless

Thanks Nigel.

exit repeat - that's exactly what I was after.

Thanks,
Olli

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)