Script for continuous screen recording

Hello.

I’m testing a GUI and sometimes events happen randomly. In order to catch them, I thought I could make a script to continuously record my screen and say every 10 minutes to save it in a file and start again. I don’t need to keep a record, so I thought to delete all files older than one hour. This is what I came up with after some googling and tinkering:


set {year:y, month:m, day:d, hours:h, minutes:min} to (current date)
repeat until h is greater than 20 -- tmp, shut down after 20:00 if I forget it.
	set loopIndex to 0
	if loopIndex is greater than 5 then
		set documentToDelete to  "Mac:Users:id:Captures:capture " & y & "." & m & "." & d & "-" & h -1& "." & min  & ".mov"
		delete documentToDelete
	end if
	set loopIndex to loopIndex + 1
	set documentName to "Mac:Users:id:Captures:capture " & y & "." & m & "." & d & "-" & h & "." & min & ".mov"
	
	tell application "QuickTime Player"
		--activate
		new screen recording
		start document loopIndex
		--delay 600 -- record 10 min
		delay 60 -- record 1 min (to debug)
		stop document loopIndex
	end tell
	tell application "System Events" to set visible of process "QuickTime Player" to false
	set saveDoc to "tell application \"QuickTime Player\" to save document " & loopIndex & " in file " & documentName & " > /dev/null>&1 &"
	--ignoring application responses
	do shell script saveDoc
	--end ignoring
	set min to (min + 10) mod 60
	set {year:y, month:m, day:d, hours:h} to (current date)
end repeat
tell application "QuickTime Player" to quit

the

command was added because I noticed it took too much time to save the file and the script froze (and did not record). This way it I thought it would be able to save it and start recording again but it does not. Instead QuickTime gets stuck and I end up with only the first file.

Is there a way to do this? I’m thinking that at least two instances of Quicktime must run at the same time, one recording and the other one saving.

Any help is appreciated.

Edit, a few hours later: While looking into something else this evening, I discovered copies of all the recordings I’d made while testing this script in my Movies folder! What this means is that the saving the script does is irrelevant, since the recordings are saved anyway while they’re in progress. I’ll leave the script in place (with one minor error correction), but this discovery should be born in mind. And if you do a lot of screen recording, your Movies folder may be due for a major purge!

Hi, derio. Welcome to MacScripter.

For your purposes, a stay-open script applet with an ‘idle’ handler would be better than a repeat. Such an applet simply hangs around doing nothing until it’s prompted by the system to execute its ‘idle’ handler. The handler returns a number when it’s finished indicating how many seconds later the applet is to be prompted again.

The script below works, but with interruptions when recordings are stopped and saved. I haven’t been able to find a way to keep QuickTime Player invisible while it’s stopping a recording or saving one. If it were possible, it would make sense to start each new recording before saving the previous one to minimise the gap in coverage. But as it is, any overlap would only contain the saving display, so the new recording may as well start after that anyway.

The files are named for when the recordings stop, but it should be possible to arrange for the names to contain the start times instead if required.

Save the script as an application, checking the “Stay open” checkbox in the “Save as.” dialog:

-- Customising properties.
property captureLength : 10 * minutes -- Length of each recording. (More accurately, the interval between their completions.)
property maxToKeep : 6 -- Maximum number of files to keep.
property stopTime : 20 * hours -- The time of day after which the script should stop.

property basePath : missing value -- Will be the capture folder path & the "capture " at the beginning of each file name.
property capturePaths : {}

-- Initialisation when the script's run.
on run
	set captureFolderPath to (path to home folder as Unicode text) & "Captures:"
	tell application "System Events" to delete files of folder captureFolderPath
	
	set basePath to captureFolderPath & "capture "
	set capturePaths to {}
	
	launch application "QuickTime Player"
end run

-- Immediately and on subsequent paging of the application by the system:
on idle
	set {year:y, month:m, day:d, time:t} to (current date)
	
	-- Is there a recording in progress?
	tell application "QuickTime Player" to set haveRecording to (document "Screen Recording" exists)
	-- If so.
	if (haveRecording) then
		-- Stop the recording and identify the document.
		tell application "QuickTime Player"
			stop document "Screen Recording"
			-- The name of the document changes when the recording stops.
			set stoppedRecording to (last document whose name begins with "Screen Recording")
		end tell
		
		-- Make up a save path for the document based on the current date and time.
		tell (y * 10000 + m * 100 + d) as text to set latestPath to basePath & (text 1 thru 4 & "." & text 5 thru 6 & "." & text 7 thru 8)
		tell (10000 + t div hours * 100 + t mod hours div minutes) as text to set latestPath to latestPath & ("-" & text 2 thru 3 & "." & text 4 thru 5 & ".mov")
		
		-- Save the document and close it.
		tell application "QuickTime Player"
			save stoppedRecording in file latestPath
			close stoppedRecording
		end tell
		
		-- Update the path list and delete the oldest capture file if there are more than needed.
		set end of capturePaths to latestPath
		if ((count capturePaths) > maxToKeep) then
			set oldestPath to beginning of capturePaths
			tell application "System Events" to delete file oldestPath
			set capturePaths to rest of capturePaths
		end if
	end if
	
	-- If it's now past the latest you want to continue, quit QuickTime Player and this script; otherwise start a new recording and ask for the script to be paged again at the end of the required recording time (less the time it's taken to execute the above).
	if (t comes after stopTime) then
		tell application "QuickTime Player" to quit
		quit
		return 1
	else
		tell application "QuickTime Player" to start (new screen recording)
		return captureLength - ((time of (current date)) - t)
	end if
end idle

Hi Nigel, derio,

Very interested in trying to make this work, but I’ve only got Python scripting skills and have been tearing my hair out for a day trying to debug it.

Upon running it (on OS X 10.8.5) as an application that stays open after run handler, it immediately closes. If I run it with Quicktime open, both the script and Quicktime immediately close.

Do you have any suggestions?

Thanks for a great resource.

J

Hi jacksongs. Welcome to MacScripter.

Just looking over the script quickly to refresh my memory, it seems it’s written to quit both QuickTime Player and itself after eight o’clock in the evening. The line at the top .

property stopTime : 20 * hours -- The time of day after which the script should stop.

. set up the finish time and this bit at the bottom .

if (t comes after stopTime) then
	tell application "QuickTime Player" to quit
	quit
	return 1
else
	tell application "QuickTime Player" to start (new screen recording)
	return captureLength - ((time of (current date)) - t)
end if

. does the quitting if it’s later than that or starts a recording if it isn’t. You can either adjust the quit time (probably best) or just keep the two lines between the ‘else’ and the ‘end if’ in the ‘if’ statement.

Hi Nigel,
Your stay-open script applet is great and very timely for a current need of mine. However, I need monitor only a specific window and don’t really want to record everything else going on my screen (others may see the recordings).

Is it possible to limit the recording to a specific window? If it is, then I’d like to:

  • hard code the window name or better, enter the name in a dialog box upon starting the applet (this is easy enough for me to script) but then;
  • the applet should detect the opening of the specific window, start recording its contents, then stop the recording when the window closes. Is this possible and if so, how?

Cheers,
Chris

Hi Chris.

Sorry I’ve been a while getting back. I’m afraid can’t see any way to record just a particular window.

Well, I just have to watch what else I’m doing while recording the screen :wink: