MacOS – Can Applescript be used to automate a screen recording session on OS X

applescriptmacosquicktime

I need to accomplish the following tasks from a script:

  1. Initiate a screen recording (with system audio).
  2. After a set delay, stop the recording.
  3. Save out to a file.
  4. Close the recording.

I've tried a few different approaches here and ran into roadblocks. One thing I've tried for example was a QuickTime script found here.

tell application "QuickTime Player"
    set newScreenRecording to new screen recording
    tell newScreenRecording
        start
        delay 10
        stop
    end tell
    set newDoc to last item of (documents whose name contains "Untitled")
    set f to "" & (path to desktop) & "quicktimeFile"
    export newDoc in f using settings preset "Quicktime Movie"
end tell

This script doesn't error, but it doesn't save anything either.

In the past I've used iShowU HD, but I've ran into significant performance issues there which have caused me to go back to the drawing board.

I'm willing to pay for a third-party application provided that it meets my requirements, so feel free to suggest anything that could get the job done.

Running OS X 10.8 Mountain Lion.

Best Answer

Try this script:

tell application "QuickTime Player"
    set newScreenRecording to new screen recording
    tell newScreenRecording
        start
        delay 3
        stop
    end tell
    tell last item of documents
        close
    end tell
end tell

It will make a new recording for 3 seconds (edit the delay 3 line to change the length), which gets automatically saved in the Movies directory of your Home directory, then closes the recording window.

If you want to automatically export to a different format, you can do that by adding the line export in ("" & (path to desktop) & "quicktimeFile.m4v") using settings preset "480p" before the close line. The presets you can choose from are defined in the File > Export dialog:

export options

Check the actual dialog for further details on the presets (note that they are "up to" the resolution listed - it will fit into the horizontal pixels available, so "480p" gives you 640x400 for a 16:10 recording). As far as I know, you can't make custom presets.

If you want to capture your system sounds, and not mic/line-in audio, check out this question: Can I get system sound along with QuickTime Player screen recording?

For the curious, there are a couple notes about why the original script doesn't work and QuickTime Player's AppleScript peculiarities:

  • The set newDoc […] line tries to grab the most recent document with the string "Untitled" to account for the newScreenRecording reference becoming invalid once the recording is stopped (which is probably a poor design decision, but oh well). However QuickTime Player under Lion (and possibly Snow Leopard) auto-saves screen recordings as "Screen Recording", "Screen Recording 2", etc., so it was finding no documents. Having it grab just the most recent document solves this, and future proofs it to a degree.

  • The export line references a preset that no longer exists (or has been renamed). Unfortunately neither QuickTime nor AppleScript gives any errors about this, it simply fails silently (more poor design).

  • The AppleScript dictionary for QuickTime Player lists a save action, but I couldn't get it working—I suspect the functionality was killed, but the dictionary entry wasn't removed. You'll note that there's no Save functionality in the GUI, only export (and auto-save). So if you want to change the save location without exporting a new file, you'll have to get the file location, then move it through the Finder.