我正在尝试减少步骤数并提高 applescript 的性能,我只是想知道是否有一些我可以使用常用功能。
这是一个示例脚本...
tell application "QuickTime Player"
activate
-- Get the iCloud file path to avoid permission error
set filePath to "Macintosh HD:Users:jm:Library:Mobile Documents:com~apple~QuickTimePlayerX:Documents:movie.wav"
set f to a reference to file filePath
-- Get a handle to the initial window
set windowID to id of first window whose name = "Audio Recording"
set audio to first document whose name = (get name of first window whose id = windowID)
tell audio
stop
end tell
-- Get second handle to new titled window
set windowID2 to id of first window whose name = "Untitled"
set audio2 to first document whose name = (get name of first window whose id = windowID2)
tell audio2
-- Save audio file
save audio2 in f
end tell
-- Get third handle to new titled window
set windowID3 to id of first window whose name = "movie.wav.qtpxcomposition"
set audio3 to first document whose name = (get name of first window whose id = windowID3)
tell audio3
close audio3 saving no
end tell
end tell
这是第二个脚本,在开始录制的脚本之后调用。
答案1
我可以将您的脚本简化为:
tell application "QuickTime Player"
-- Get the iCloud file path to avoid permission error
set filePath to "Macintosh HD:Users:jm:Library:Mobile Documents:com~apple~QuickTimePlayerX:Documents:movie.wav"
-- Get a handle to the initial window
stop the document named "Audio Recording"
-- Get second handle to new titled window
save the document named "Untitled" in filePath
-- Get third handle to new titled window
close the document named "movie.wav.qtpxcomposition" saving no
end tell
id
正如我在评论中提到的,通过窗口的 来检索窗口的name
,然后从name
中检索 ,这是多余的id
。您可以document
通过已有的名称引用 (如果不存在该名称的文档,则会引发错误;但您的原始脚本也是如此)。为了避免这种情况,您可以先检查它是否存在:
tell document named "Audio Recording" to if it exists then stop
该activate
命令似乎没有必要,因为后面的命令都不需要QuickTime成为焦点。
最后,该变量f
是多余的。
编辑
添加于2019-11-07
关于:以下是 OP 的第一条评论
概括:通过其窗口id
而不是通过引用文档name
来提高稳健性。
tell application "QuickTime Player"
set filePath to "Macintosh HD:Users:jm:Library:Mobile Documents:com~apple~QuickTimePlayerX:Documents:movie.wav"
set windowID to the id of window 1 where the name of its document is "Audio Recording"
tell the document of window id windowID
stop
save in filePath
close
end tell
end tell