我在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
这是我如何等待用户手动停止记录,然后继续执行代码的其余部分:
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 access
和close access
命令是标准添加的一部分并且实际上在tell application "QuickTime Player"
块中包装时无声地出错,以及为什么它们被分开。这避免了浪费CPU循环,因为在tell current application
下不需要第二次调用它们,所以可以正确地纠正无声错误。
作为编码,这里假设只有一个QuickTime Player窗口正在进行音频录制一次,并保持QuickTime Player的最前端窗口.
如果User点击关闭new audio recording
窗口上的按钮Cancel单击按钮,脚本停止并保存录制。
在repeat
循环中调整delay
命令的值,如果您觉得合适的话。
在macOS Catalina下测试与语言&地区set toEnglish (US)inSystem Preferences并为我编码。
<子>注意:示例AppleScriptcode就是这样,并且sans任何包含的错误处理不包含任何额外的错误处理可能是适当的。用户有责任根据需要添加任何错误处理。看看try语句和错误AppleScript语言指南中的语句. 参见处理错误. 此外,延迟的使用在适当的情况下,在事件之间可能需要命令,例如delay 0.5
,适当设置延迟的值。子>