Home > Enterprise >  Applescript, repeat over the first N items in a list?
Applescript, repeat over the first N items in a list?

Time:10-30

I want to repeat over the first 50 items in a list with 150 items/objects. Is there some shortcut or smart way achieve this in AppleScript. I know of

repeat with theObject in theObjectList

as well ass

repeat 50 times

Can they be combined somehow?

CodePudding user response:

My suggestion is to use an index based repeat loop. If the number of items in the list is less than 50 the repeat loop will just complete, otherwise the loop will be left after 50 iterations.

repeat with i from 1 to (count theObjectList)
   set theObject to item i of theObjectList
   —- do something
   if i is 50 then exit repeat
end repeat

CodePudding user response:

You can also specify a range of items to get from the list, but you need to check that you don’t go out of bounds, for example:

set someNumber to 50
set theCount to (count theObjectList)
if someNumber > theCount then set someNumber to theCount

repeat with theObject in (items 1 thru someNumber of theObjectList)
   log theObject
end repeat
  • Related