System events got an error: Connection is Invalid


I’m experimenting to make datamoshing video’s, a little bit like so:
First I prepare the video, and then I’m moving the cursor backwards and forwards in various patterns.
The script I wrote does something like this: System events tells quicktime to move 5 frames forward, 5 frames backwards, make a screenshot and advance one frame. This small code loops over and over.
When I do short test-clips of a few hundred frames, everything goes fine, but when I let the computer work overnight on thousands of frames, applescript always stops somewhere in between with the error “System events got an error: Connection is Invalid”.

In ‘Activity Monitor’ I see that applescript is taking up all the available memory (the computer has 8Gb of Ram, and applescript took up 5.5Gb. That combined with the other memory usage, almost no free memory was available), making the computer extremely slow. Apparently the script accumulates memory over time. I have no idea why this is happening, but I hope this problem is connected to the invalid connection error.

I thought by increasing the wait-time in between every command I would increase the reliability, but that doesn’t seem to be the case.
Is there something I can try to make it more stable?

Here’s the full script I’m currently working on:

(*	An Van Dienderen: Letter Home: Datamosh
	The script assumes there is a quicktime file opened in QuictTime Player 7
	see for keyboard codes:
	Cloud wiggle:
	1. make a pixel-cloud by playing forward and backwards a part of the video
	2. play a part of the video in a regular fashion to allow new pixel-information to go over the pixel cloud, thus creating a combination of glitch-esthetics and regular video footage.

set filename to "cloud_10_"
set expected_fps to 25

set cloud_inpoint to 3680 -- (framenumber) in-punt voor het maken van de cloud
set cloud_size to 30 -- (framecount)how much frames will be used to make the pixel-wiggle-cloud

set run_inpoint to 11121 -- (framenumber) in-punt voor het stukje gewoon afspelen
set run_size to 40 -- (framecount) how much frames will be used to make the regular-run
set run_outpoint to 80880 -- (framenumber) to stop the process. Can be used to define a single scene in a larger video

set cursor to 0
set repetition to 0
set wait to "sleep .03"
set loc to "/Users/herculeslab/Desktop/screenshots/" -- screenshots folder
property n : 0

----- initialisation
tell application "QuickTime Player 7" to activate

tell application "System Events"
	tell process "QuickTime Player 7" to set frontmost to true
	set n to 0
	tell application "QuickTime Player 7"
		set movie_timescale to time scale of document 1 -- reset each time to frame 1
		set multiplier to round (movie_timescale / expected_fps)
		set current time of document 1 to 0
	end tell
	----- / initialisation
	repeat while cursor ≤ run_outpoint
		-- reset
		tell application "QuickTime Player 7" to set current time of document 1 to 0
		-- make a pixel-cloud
		repeat with i from cloud_inpoint to (cloud_inpoint + cloud_size) by 1
			tell application "QuickTime Player 7" to set current time of document 1 to (i * multiplier)
			do shell script wait
		end repeat
		do shell script wait
		repeat with i from ((cloud_inpoint + cloud_size) - 1) to cloud_inpoint by -1
			tell application "QuickTime Player 7" to set current time of document 1 to (i * multiplier)
			do shell script wait
		end repeat
		do shell script wait
		-- play a part of the video regularly
		repeat with i from (run_inpoint + repetition) to (run_inpoint + repetition + run_size) by 1
			set cursor to i
			tell application "QuickTime Player 7" to set current time of document 1 to (i * multiplier)
			do shell script wait
		end repeat
		do shell script wait
		do shell script wait
		do shell script wait
		do shell script wait
		-- make screenshot
		set n to n + 1
		set picPath to (loc & filename & n & ".png") as string
		do shell script "screencapture " & quoted form of picPath
		--  & " &> /dev/null &" : geen response waarop applescript zal wachten
		set repetition to repetition + 1
	end repeat
	do shell script "sleep .5"
end tell

thanks a lot in advance!



I don’t know the cause of your problem, but one obvious point to mention is that System Events is entirely superfluous in your script. The only System Events command there is ‘tell process “QuickTime Player 7” to set frontmost to true’ ” and that action’s already taken care of by ‘tell application “QuickTime Player 7” to activate’. It wouldn’t hurt for a start to remove the ‘tell application "System Events’ and ‘tell process “QuickTime Play 7” .’ lines and the ‘end tell’ at the bottom of the script.

“Connection is Invalid” errors are often caused by applications suddenly quitting while being sent commands

Thanks Nigel!

Interesting! In previous versions instead of setting the current time I just sent keyboard-commands to quicktime (right arrow, left arrow)… That’s probably the reason it was still in there.
I forgot to mention that quicktime did not quit, but System Events might have done so…
I removed the system events commands entirely.

I’m curious to see what happens overnight!

It’s two days later now. I take a look at the computer again and I find all programs closed. Some crash must have occurred. The script has made 6480 screenshots though, that’s better than previous attempts!

In the console window I think I see the computer restarting after about 27 hours of running the script.
Right before the kernel-initialization lines I see:

Googling this thing I found that this line is often found by crashes, although one article stated that this is a regular startup-message. Another article ( ) suggested to validate the fonts on the computer. I don’t see any relation to the problem but I validated all the fonts on the computer anyways.

Since the computer restarted I have no idea if the memory had a buildup or stayed stable.

I’m off to let the script run again. Hopefully without a crash this time.