0

I'm trying to reduce the number of steps and and increase performance for my applescript, I just wondered if there are some common functions I can employ.

Here's an example script...

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

This is the second script, called after a script which starts recording.

0

1 Answer 1

0

I can reduce your script to this:

    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

As I mentioned in my comment, it's redundant to retrieve a window's id by its name, only then to retrieve its name from that id. You can reference the document by the name you already have (if no document by that name exists, it will throw an error; but the same is also true of your original script). To avoid this, you could check it exists first:

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

The activate command seemed unnecessary, as none of the commands that followed require QuickTime to be in focus.

Finally, the variable f was redundant.

EDIT

Added on 2019-11-07
Re: first comment below by OP
Summary: References document through its window's id rather than by name for increased robustness.

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
4
  • The close the document named "movie.wav.qtpxcomposition" saving no no longer works, on Mojave it show a Finishing Recording.. dialog which never finishes... Any ideas?
    – Jules
    Commented Nov 7, 2019 at 19:04
  • I don't use Mojave, but reviewing the script now, I think references by name to a document whose name keeps changing is fragile. The id should remain constant, in theory, so we can grab it once and use it exclusively. So similar in approach to your original script, but just one-third of it. I'll add an edit to the answer, but it's a shot in the dark as to whether it will change the situation. If you uploaded a photo to Imgur with a screenshot cropped to show the entire contents of Script Editor's Replies pane up to where you terminate the script, it might enlighten.
    – CJK
    Commented Nov 7, 2019 at 19:34
  • Thanks, the Replies panel doesn't show anything additional. However I've just found quit which seems to work.
    – Jules
    Commented Nov 7, 2019 at 19:58
  • 1
    Ok, well done. 👍
    – CJK
    Commented Nov 7, 2019 at 20:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .