Quicktime Player onLoadFinished event?

I have this system, consisting of a bunch of different applications/scripts (website, database, Max/MSP) including an applescript to trim video’s to a certain size. But somehow, some video’s didn’t get trimmed and remained their full length. I found out the problem is caused by the applescript. Take a look at the following lines (which is just a small part of the script, but the part with the problem):

tell application "QuickTime Player"
	open item 1 of argv
	select at (item 2 of argv) to (item 3 of argv)
	trim
(...)

AKA open the file specified in the 1st parameter, select the part between the 2nd and 3rd parameters (which are miliseconds, taking into account the timescale of the movie (600)) and trim the movie to the selected part. But as said, some video’s don’t get trimmed.

I found out what the problem is; when you run the script the file opens, but it seems some files take a little time getting fully loaded; you can see the time-line getting filled from left to right. But the trim script will be executed immediately after opening the file; causing files which are not fully loaded yet to ignore the trim command and stay at their full length (you’ll just see the selection in the time-line disappear).
I’m now using a simple timeout between the opening and trimming of the file:

tell application "QuickTime Player"
	open item 1 of argv
	delay 3
	select at (item 2 of argv) to (item 3 of argv)
	trim
(...)

But ofcourse, this isn’t really good practice: 3 seconds can be way too much for some files, but could be too short for some others. Also, I need the script too execute as fast as possible; every second of delay is a second too much.

Doesn’t the Quicktime Player have some sort of onLoadFinished event? Or does anyone know a different way to see if the file is fully loaded, and then execute the trimming script?

Maybe load state will work for you. When I load large FLV movie, it can take some time for it to fully load (fully fill the position bar). While it is working the load state is playthrough okay. Your values might be different depending on what files you are loading.

tell application "QuickTime Player"
	--  load
	-- load state values from QTP dictionary: load state unknown/load error/loading/loaded/playable/playthrough okay/complete
	set errorStates to {load state unknown, load error}
	set waitStates to {loading, playable, playthrough okay}
	set finalStates to {loaded, complete}
	repeat while true
		set theState to load state of document 1
		if theState is in finalStates then exit repeat
		if theState is in errorStates then error "Entered an ˜error' load state: " & theState as string
		if theState is not in waitStates then
			-- error? log? ignore?
		end if
		delay 0.1
	end repeat
	-- trim
end tell

Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari 4.0.3 (4531.9)
Operating System: Mac OS X (10.4)