AppleScript - 高级功能,用于改进步骤/减少代码


0

我正在尝试减少步骤的数量并提高我的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

这是第二个脚本,在开始录制的脚本之后调用。


为什么你的语句首先通过名称检索窗口的id,然后是通过id检索同一窗口名称的语句?这看起来有点多余。
CJK

Answers:


0

我可以将你的脚本减少到这个:

    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,才检索其nameid。您可以document通过您已有的名称引用(如果不存在该名称的文档,则会引发错误;但原始脚本也是如此)。为避免这种情况,您可以先检查它是否存在:

    tell document named "Audio Recording" to if it exists then stop

activate命令似乎没有必要,因为后面的命令都不需要QuickTime成为焦点。

最后,变量f是多余的。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.