Last time I was trying to [blog:capturing-video-with-applescript-and-quicktime capture video using AppleScript and QuickTime] and running into all sorts of problems. I spent many hours trying to work around the issues. I finally broke down and just used Sikuli to do the job... though there were some interesting issues with this as well.
Against the script is broken into two parts so they can be executed from a shell script.
The first part is pretty simple:
All this does is start the QuickTime player, start a new screen recording, and start the recording. There's one "interesting" bit in the script though... it moves the recording window to a specific location before starting it. "Why? Doesn't Sikuli support searching the screen to do image matching to find UI elements?" Well the answer is that while Sikuli does that once the screen recording is started the QuickTime gui becomes invisible to Sikuli. I'm guessing it uses the same techniques that Sikuli itself uses to hide its interface from being detected while doing screenshots. Either way by putting the UI at a specific position that lets us find it again (provided it wasn't moved by another script of the user).
Here's the stop script:
Again, this is all pretty straight forward. We open QuickTime and start looking for the "Closing... do you want to save?" dialog and then click where we moved the UI, close QuickTime and click the
dialog that pops up. The only interesting thing to note here is the if exists() block. I've used this technique as an alternative to an explicit wait or a wait with a timeout on many occasions. I find it works quite well if you're having problems with some UI element popping up quickly enough.
Putting this all together I wrote a little testing script to run through the process:
echo "STARTING VIDEO CAPTURE"
java -jar "/Applications/Sikuli-IDE.app/Contents/Resources/Java/sikuli-script.jar" -s
"/Users/benstartcapture.sikuli"
sleep 10
echo "STOPPING VIDEO CAPTURE"
java -jar "/Applications/Sikuli-IDE.app/Contents/Resources/Java/sikuli-script.jar" -s "/Users/ben/stopcapture.sikuli"
Just replace the sleep with whatever you need to do. Obviously this will need some modifications to work on other OSes.
I've uploaded the scripts I wrote to this post if you want to investigate them further.