首页 > 解决方案 > 有没有办法用 quicktime 和 applescript 暂停录音和恢复?

问题描述

我在 AppleScript 中尝试过这个:

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

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

标签: applescriptaudio-recordingquicktime

解决方案


好的,我对此进行了一些研究,并提出了以下脚本。如果音频录制窗口打开,则此脚本将在录制暂停时开始或恢复录制,如果不是,则暂停录制。这使用 python 来模拟选项键按下(按住选项键将“停止”按钮变为“暂停”按钮),因此您可能需要安装 python 的 Objective-C 包。有关详细信息,请参阅此 StackOverflow 答案我刚刚发现 PyObjc 2.5 默认安装在 OSX 上,这对于此目的应该绰绰有余。

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 需要在前台捕捉按键事件,否则暂停将不起作用。我想我可以对其进行调整,以便按键事件直接进入 Player 应用程序,但我必须对其进行研究。此外,我没有以任何方式编写代码来停止录制(尽管这很容易:只需单击没有controlKeyEvent 调用的按钮)。我不确定你的工作流程是什么样的,我不想通过发出不方便的警报来破坏它。


推荐阅读