【问题标题】:Audio recording in QuickTime with Apple Script使用 Apple Script 在 QuickTime 中录制音频
【发布时间】:2021-02-19 02:16:41
【问题描述】:

我在 QuickTime Player 中运行 AppleScript 来执行音频录制。当用户单击按钮停止录音时,我如何等待片刻?我们需要将“延迟5”替换为等待。

set filePath to (path to desktop as text) & "test.m4a"
delay 1
tell application "QuickTime Player"
    activate
    set new_recording to (new audio recording)
    tell new_recording
        start
        delay 5
        stop
    end tell
    
    open for access file filePath
    close access file filePath
    export (first document) in filePath using settings preset "Audio Only"
    close (first document) without saving
    ignoring application responses
        quit
    end ignoring
end tell

【问题讨论】:

    标签: applescript quicktime


    【解决方案1】:

    这就是我等待用户手动停止录制然后继续并继续其余代码的方式:

    set filePath to (path to desktop as text) & "test.m4a"
    
    tell application "QuickTime Player"
        activate
        set newRecording to (new audio recording)
        set docName to name of newRecording
        tell newRecording to start
        repeat while document docName exists
            set clickedCancelButton ¬
                to my checkForSheet1AndClickCancel()
            if clickedCancelButton then ¬
                tell application "QuickTime Player" to ¬
                    tell newRecording to stop
            delay 1
        end repeat
    end tell
    
    open for access file filePath
    close access file filePath
    
    tell application "QuickTime Player"
        export (first document) in filePath ¬
            using settings preset "Audio Only"
        close (first document) without saving
        quit
    end tell
    
    on checkForSheet1AndClickCancel()
        tell application "System Events"
            tell application process "QuickTime Player"
                if (exists sheet 1 of window 1) then
                    click button 2 of sheet 1 of window 1
                    return true
                else
                    return false
                end if
            end tell
        end tell
    end checkForSheet1AndClickCancel
    

    注意事项

    open for accessclose access 命令标准添加 的一部分,当包裹在 tell application "QuickTime Player" block 以及他们分开的原因。这避免了浪费的 CPU cycles 因为不必在tell current application 下再次调用它们而对静默错误太正确了。

    按照编码,这假设一次只有一个 QuickTime Player 窗口 进行音频录制,并且仍然是最前面的QuickTime Player的>窗口

    它也被编码,所以如果用户点击new audio recording 窗口上的关闭 按钮 取消 按钮被点击,脚本停止并保存录音。

    调整repeat循环delaycommand,你觉得合适。

    这是在 ma​​cOS Catalina 下测试的,其中 Language & RegionSystem Preferences 中设置为 English (US)并按照编码为我工作。


    注意:示例 AppleScript 代码 就是这样,并且没有任何包含的错误处理不包含任何适当的额外错误处理。用户有责任根据需要或需要添加任何错误处理。查看AppleScript Language Guide 中的try statementerror statement。另请参阅Working with Errors。此外,在适当的情况下,可能需要在事件之间使用delay 命令,例如delay 0.5延迟设置得当。

    【讨论】:

    • 感谢您的建议!它几乎可以工作,但我们发现了两个问题:1.当我们检查“录音”标题时,它适用于macOS的英文界面。它不适用于德语或法语。 2. 如果用户在没有按停止录制按钮的情况下单击 X 按钮关闭 QuickTime,则应用程序提供保存音频文件,然后 QuickTime 窗口冻结。
    • @Igor,我添加了代码来说明new audio recording窗口 的名称和一个停止并保存记录的处理程序如果用户按下new audio recording窗口 上的关闭按钮。如果这不能解决您的问题,请告诉我,我会永久删除它。
    • @Igor,更新答案是否满足您的需求?
    • 再次感谢您的慷慨帮助!遗憾的是,更新后的脚本在我的 Mac 上无法正常工作。单击停止按钮时,它会保存一个空的 .m4a 音频(0 字节),并且文件始终保存在桌面上,即使我设置了完整文件路径“/Users/mac/Documents/test.m4a”
    • @user3579815,老实说,我更希望您自己提出,因为这是我回答涉及编码问题的风格的一部分。也就是说,我当然不能因为帖子内容的许可而阻止你做你想做的事:What is the license for the content I post?
    猜你喜欢
    • 2014-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-29
    • 2015-11-06
    • 1970-01-01
    相关资源
    最近更新 更多