'Control Quicktime Using Applescript

I am looking for a solution to control Quicktime using Applescript. All I need to do is - - open quicktime and start a recording - stop the recording and save the file to a set location.

From previous posts I managed to get the following.

    set theFilePath to "/Users/jamestebb/Desktop/01TestRecords/ " & "myfile" & ".mov"

tell application "QuickTime Player"
    set newMovieRecording to new movie recording
    tell newMovieRecording
        start
        delay 5
        pause
        save newMovieRecording in POSIX file theFilePath
        stop
        close newMovieRecording
    end 

This works perfectly however, for this project I need separate script cues to start and stop the recording as opposed to having a wait / delay.

To start the recording I used.

set theFilePath to "/Users/jamestebb/Desktop/01TestRecords/ " & "myfile" & ".mov"

tell application "QuickTime Player"
    set newMovieRecording to new movie recording
    tell newMovieRecording
        start

    end tell
end tell

This works fine however when I try to stop the recording using the below I get errors where Pause is not understood

set theFilePath to "/Users/jamestebb/Desktop/01TestRecords/ " & "myfile" & ".mov"

tell application "QuickTime Player"
    set MovieRecording to new movie recording
    tell MovieRecording

        pause
        save MovieRecording in POSIX file theFilePath
        stop
        close MovieRecording
    end tell
end tell

Im guessing that this is because the file currently recording is no longer a "new movie recording"

Any ideas soon how to fix??



Solution 1:[1]

I used the following example AppleScript code to test starting a New Movie Recording in QuickTime Player with the first script, and then processed it with a second script to pause, save, stop and close it.

  • Note that this assumes QuickTime Player is running with just the one New Movie Recording and no other QuickTime Player windows open.

Script 1:

tell application "QuickTime Player" to start (new movie recording)

Script 2:

set theFilePath to POSIX path of (path to movies folder) & "myMovie.mov"

tell application "QuickTime Player"
    tell document "Movie Recording"
        pause
        save it in POSIX file theFilePath
        stop
        close
    end tell
end tell

Between these two scripts it successfully created the myMovie.mov file in the Movies folder of my Home folder.

Testing was done under macOS High Sierra.


Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

Solution 2:[2]

The script does not work for my older Macbook Air especially for longer recordings.

So, I learned about "sox". (this is for audio recordings only). I use Applescript and integrate that with recording with Terminal. Abort the recording with Control + C.

tell application "System Events"
    tell application "Terminal" to activate
    delay 1
    repeat 2 times
        delay 2
        key code 8 using {control down}
    end repeat

end tell

Mac equivalent of arecord command?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 S.Doe_Dude