Loops: Looping 400 times, Executes another script every 50th loop

Hi there,

I’m writing a script to loop through items on a paginated page.
There are fifty items per page. I know how many total items are in the list but the list is paginated.

lets say there are 400 items in the list (this number could change).

I have my loop to run 400 times, but on the 50th, 100th loop etc. I need it to trigger the script to hit the ‘next page’ button on every fiftieth loop.

Is this possible?

Hi,

it’s pretty easy with the modulo operator

repeat with i from 1 to 400
	if i mod 50 is 0 then hitNextPage(i)
end repeat

on hitNextPage(i)
	log i
end hitNextPage

Something like this?

set numberOfItems to 431
set pageSize to 50

repeat with x from 0 to numberOfItems div pageSize
	repeat with i from 1 to pageSize
		set theIndex to x * pageSize + i
		if theIndex > numberOfItems then exit repeat
		-- insert code here
	end repeat
end repeat

Thank you both!!

I went with StefanK’s solution as it was more compact, but I tried both solutions and they both work for me.