有没有一种方法可以暂停音频记录并使用quicktime和applescript恢复?

时间:2020-01-24 06:23:07

标签: applescript audio-recording quicktime

我在AppleScript中尝试过此操作

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

启动QuickTime播放器并开始录制时,它不会暂停,但是有没有办法使用AppleScript和QuickTime播放器来暂停→播放→暂停等来录制音频?

1 个答案:

答案 0 :(得分:1)

好吧,我对此进行了深入研究,并提出了以下脚本。如果打开了“录音”窗口,则如果暂停录音,此脚本将开始或继续录音;如果不暂停,则暂停录音。这使用python模拟选项键的按下(按住选项键将“停止”按钮变成“暂停”按钮),因此您可能需要安装python的Objective-C软件包。有关详细信息,请参见this StackOverflow answer。我刚刚发现OSX默认安装了PyObjc 2.5,对于这个目的,这已经足够了。

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

现在,Quicktime Player必须位于前台才能捕获按键事件,否则暂停将无法进行。我想我可以进行调整,使按键事件直接进入播放器应用程序,但我必须对此进行研究。另外,我也没有以任何方式停止录制(尽管这很容易:只需单击没有 controlKeyEvent调用)。我不确定您的工作流程是什么样,也不想通过引发不便的警报来破坏它。