applescriptaudio-recordingquicktime

Is there a way to pause audio recording and resume with quicktime and applescript?


I was trying this in AppleScript:

tell application "QuickTime Player"
    activate
    set doku to new audio recording
    start doku
    delay 4
    pause doku
end tell

While its starting the QuickTime player and starting the recording it does not pause, is there a way to pause → play → pause etc with the AppleScript and QuickTime player for recording audio?


Solution

  • Ok, I dug into this a bit and came up with the following script. If an Audio Recording window is open, this script will start or resume recording if recording is paused, and pause recording if it isn't. This uses python to simulate option-key presses (holding down the option key turns the 'stop' button into a 'pause' button), so you may need to install python's Objective-C packages. See this StackOverflow answer for details: I just discovered that PyObjc 2.5 is installed on OSX by default, and that should be more than sufficient for this purpose.

    tell application "QuickTime Player" to activate
    
    tell application "System Events"
        tell process "QuickTime Player"
            tell window "Audio Recording"
                set actionButton to first button whose description is "stop recording" or description is "start recording" or description is "resume recording"
    
                if description of actionButton is in {"start recording", "resume recording"} then
                    -- start/resume audio recording
                    click actionButton
                else if description of actionButton is "stop recording" then
                    -- pause audio recording
                    my controlKeyEvent(true)
                    click actionButton
                    my controlKeyEvent(false)
                end if
            end tell
        end tell
    end tell
    
    on controlKeyEvent(isKeyDown)
        if isKeyDown then
            set boolVal to "True"
        else
            set boolVal to "False"
        end if
        do shell script "
    
    /usr/bin/python <<END
    
    from Quartz.CoreGraphics import CGEventCreateKeyboardEvent
    from Quartz.CoreGraphics import CGEventCreate
    from Quartz.CoreGraphics import CGEventPost
    from Quartz.CoreGraphics import kCGHIDEventTap
    import time
    
    def keyEvent(keyCode, isKeyDown):
        theEvent = CGEventCreateKeyboardEvent(None, keyCode, isKeyDown)
        CGEventPost(kCGHIDEventTap, theEvent)
    
    keyEvent(58," & boolVal & ");
    
    END"
    end controlKeyEvent
    

    Right now, Quicktime Player needs to be in the foreground to catch the keypress event, otherwise pausing won't work. I think I can tweak that so that the keypress event goes straight to the Player app, but I have to look into it. Also, I didn't code in any way to stop recording (though that would be easy enough: just click the button without the controlKeyEvent call). I wasn't sure what your workflow looked like, and I didn't want to disrupt it by throwing in an inconvenient alert.