Repeat loop problem

I got a program that can be controlled with applescript, so i build a simple repeat loop where i identify off-line clips
and remove them, the script exits with an errorcode and i understand why, but cannot resolve it.

tell application "OnTheAir Video"
	tell front playlist
		set x to clip count
		repeat with y from 1 to x
			set clipIsValid to is valid of clip y
			if clipIsValid = 0 then
				delete clip y
			end if
		end repeat
	end tell
	tell front playlist
	end tell
end tell

I get a value of x from clip count(let’s say there are 10 clips in the playlist so x=10)
then repeat from 1 to x(10) with y increment…

Let’s say that clip#5 is off-line, the clipIsValid returns a 0 for clip 5, and will be deleted in playlist, remaining clips is now nine, when the 10th repeat loop is executed, it raises an error when ‘if valid’ function is called because it asks if clip#10 is valid while there are nine.

I’ve tried to dynamically update the value of x when a clip is deleted, but that didn’t work…
Could someone tell me on how to solve this?

Hello and welcome to Macscripter! :slight_smile:

You have already spotted the error, when you come to clip nr 10, you address a clip that isn’t there, since you have deleted one from before.

I have restructured your repeat loop, maybe wrongfully, since I don’t have the app, but you should get the principle, and adapt it into something workable:

I create a list of every clip, and then I repeat over every item of that list, that way there will be no invalid index.

The code below, has not been compiled:

repeat with a in every clip -- or similiar
	if is valid of a then 
		delete a
	end if
end repeat

You could use a descending loop index instead of an ascending one:

repeat with y from x to 1 by -1
	-- etc.

That is much better and easier for the OP to implement. :slight_smile:

Thanks Nigel for your efford in solving this issue… it works like a charm!