Repeat loop stops at random in middle

OK, I have a big script that works on Photoshop CS3 files. These files contain some layer sets, miscellaneous layers, and 60 color layers that all start with “COLOR:”. I have a repeat loop that works through the COLOR layers one by one: it flattens and resizes, exports the file as a JPEG, resets the history to just before flattening, deletes the color layer when finished with it, and moves on to the next color.

Here’s the problem: the script gets an error in the middle of the repeat loop and can’t process the 37th color layer (Country Stone - “CS”). I think it is not resetting the history after flattening, so it can’t “get” the color layer. I did some testing and here are my results:

I deleted 1 of the color layers before “CS”: script still stops at “CS”
I deleted 4 of the color layers before “CS”: script still stops at “CS”
(At this point, I’m thinking there is a problem with the “CS” layer)
THEN, I deleted 7 of the color layers before “CS”: the repeat loop continues through “CS” and stops at the 7th layer after “CS”!
Same with deleting 10 and 20 layers before “CS”: script stops at 10th and 20th layer after “CS”

Next, I deleted the layer styles of a bunch of the color layers before “CS” to reduce the size of the file the script is working through, thinking Photoshop can’t keep up with the script or something: the repeat loop actually stops only 6 layers in.

I can post some of the script if I need to (it’s really long), but I can’t think of anything in the code that could cause these types of problems. Is it a problem with Photoshop or my computer not keeping up with the script? Has anyone heard of this kind of thing happening?

Model: Mac Pro - Dual Core Intel Xeon, 2.66 GHz, 8GB RAM
AppleScript: 2.0.1
Browser: Safari 530.19
Operating System: Mac OS X (10.5)

UPDATE: I ran the same script with the same file on another, slower computer, and the repeat loop snagged much sooner. So it seems like the processing power of the computer actually affects Photoshop’s ability to keep up with the script. Do I seriously need to SLOW DOWN my script so it will run all the way through?!?

Without seeing your code it’s hard to say for sure, but I suspect that you are repeating through all of the layers, but during the repeat you are deleting layers? This will cause the repeat loop to error as the repeat is still trying to go through all the layers but now some of them don’t exist.

But htat’s just a guess. If you can post your code of at least the repeat loop in question, you might get some more feedback.

Im with Matt-Boy on this it sounds very much like you are getting a reference to list your layers then deleting from first thus at the half way point you are targeting an object that is now out of range as the indexes are recalculated at each move, add & delete. In most cases of working with files where I remove objects I work in a reverse loop so this does NOT happen.

repeat with i from Some_Count to 1 by -1

You say your script is long have you checked the amount of history steps you have Photoshop set up to keep saved?
You can alter this in the settings.

tell application "Adobe Photoshop CS2"
	activate
	tell settings
		set number of history states to 50
	end tell
end tell

Posting your script and a link to a sample file would be a help

I figured this out yesterday. As I thought, there was nothing wrong with the repeat loop. You were right, I had to set the number of history states higher. I was getting different results on different computers because their Photoshop preferences were different. BTW, within the repeat loop, I am resetting the history state, THEN deleting the color layer the script had just finished working on before going on to the next one. Doing it this way speeds the script up quite a bit as it works down the list of colors.