Segmenting a list

I have a script which uses FindFilesX to find items within a folder selection and generate a folder of aliases to the matching items. FindFilesX is set to return a list of paths to the found items.

I am unable to generate aliases to more than ~900 items at once, as it causes the Finder to crash. I have handled it by making each alias one at a time through a repeat sequence. I know this is a very slow way of doing it, so…

Is there a way to automate the breaking of this list into manageable chunks? i.e. I have 3,500 results and would like to divide it into segments of 700 breaking it into 5 chunks. The number of results will vary.

Thanks!

Use “by” in a repeat statement. Eg:

set longList to {1, 2, 3, 4, 5, 6, 7, 8, 9}

repeat with i from 1 to (count longList) by 4
	try
		set littleList to items i thru (i + 4) of longList
	on error --> end of list, can't pick 4 items
		set littleList to items i thru -1 of longList
	end try
	--> do whatever to chunk of list called "littleList"
end repeat

This will pick every iteration 4 items of the list (you can adjust it to whatever number you like). In this example: {1,2,3,4} – {5,6,7,8} – {9}

Thank you jj! That worked great.

However, I don’t understand this portion of the code very well and have seen it used before in other AppleScripts. :oops:

set littleList to items i thru -1 of longList

Would you or some other kind soul mind explaining it?

A negative number is a reference to the object at that index from the END of the list, rather than the front. So, -1 references the last item in the list. Similarly, -4 would reference the 4th item from the end of the list.

Using jj’s example, if the list has nine items, it will “try” to get little lists in chunks of 4 items. If it gets an error, probably meaning that there are not 4 items left to get, it will then try to make the little list out of the current item (i) through whatever is left in the list… here, items 9 through -1 (9th item). If you had 10 items, you’d still be getting 9 through -1, but -1 would be the 10th item.

j

Thanks jobu.

So would this command:

set littleList to items i thru -1 of longList

mean the same as this? :?

set littleList to items i thru end of longList