AppleScript - Erweiterte Funktionen zur Verbesserung von Schritten / zum Reduzieren von Code

413
Jules

Ich versuche, die Anzahl der Schritte zu reduzieren und die Leistung für mein Applescript zu steigern. Ich habe mich nur gefragt, ob es einige allgemeine Funktionen gibt, die ich verwenden kann.

Hier ist ein Beispielskript ...

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 

Dies ist das zweite Skript, das nach einem Skript aufgerufen wird, das mit der Aufnahme beginnt.

0
Warum haben Sie Anweisungen, die zuerst die ID eines Fensters anhand ihres Namens abrufen, gefolgt von Anweisungen, die denselben Namen des Fensters anhand seiner ID abrufen? Das scheint ein bisschen überflüssig. CJK vor 6 Jahren 0
Guter Punkt, danke Jules vor 6 Jahren 0

1 Antwort auf die Frage

0
CJK

Ich kann dein Skript darauf reduzieren:

 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 

Wie ich in meinem Kommentar erwähnt habe, ist es überflüssig, ein Fenster idvon seinem nameabzurufen, nur um es namevon diesem Fenster abzurufen id. Sie können auf den documentNamen verweisen, den Sie bereits haben (wenn kein Dokument mit diesem Namen vorhanden ist, wird ein Fehler ausgegeben; das gleiche gilt jedoch auch für Ihr Originalskript). Um dies zu vermeiden, können Sie zunächst prüfen, ob es existiert:

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

Der activateBefehl schien unnötig zu sein, da für keinen der folgenden Befehle QuickTime im Fokus stehen musste.

Schließlich war die Variable fredundant.