2016-05-16 57 views
0

我正在使用this AppleScript,打算在OS X上激活QuickTime並自動開始錄製音頻。用於QuickTime錄製的AppleScript:「AppleEvent處理程序失敗」

當運行中的Automator調試,腳本似乎在線路"set lastLength to duration of x"突然停止,並拋出錯誤:

"AppleEvent handler failed".

什麼可能是錯誤的腳本?

on run {input, parameters} 

    tell application "QuickTime Player" 
     activate 
     set x to new audio recording 
     start x 
     delay 1 
     set lastLength to duration of x 
     delay 1 
     set newLength to duration of x 
     try 
      repeat while lastLength is not equal to newLength 
       delay 1 
       set lastLength to newLength 
       set newLength to duration of x 
      end repeat 
     end try -- display alert name of document 1 
     set v to file of front document 
     set audioNotePath to "/Users/me/Dropbox/Inbox/- Voice Memos" 
     set thePath to POSIX path of audioNotePath 
     set dateVariable to do shell script "date '+%m.%d.%y'" 
     set timeVariable to do shell script "date +'%l.%M %p'" 
     tell x to activate 
     delay 1 
     tell application "System Events" 
      keystroke "w" using {command down} 
      delay 1 
      keystroke thePath 
      delay 1 
      keystroke return 
      delay 1 
      keystroke "AudioNote " 
      keystroke dateVariable 
      keystroke " " 
      keystroke timeVariable 
      delay 1 
      click pop up button "Format:" of group 1 of sheet 1 of window "Untitled" of application process "QuickTime Player" of application "System Events" 
      delay 1 
      key code 125 
      delay 1 
      keystroke return 
      delay 1 
      keystroke return 
      delay 1 
      open thePath 
     end tell 

     return input 
    end tell 
end run 
+1

我同意喬恩關於空閒處理程序和保存命令。最重要的是,請注意,QT文檔屬性(當前時間,數據速率,持續時間...)在錄製時不可用:這是腳本失敗的地方 – pbell

+1

對不起,意外刪除了我的評論而不是編輯。正如pbelI所說,我建議使用空閒處理程序和導出命令來保存文件,而不是運行自己的循環來確定持續時間或使用UI腳本進行保存。 pbell也是正確的,只有在您停止錄製後才能獲得大多數QT文檔屬性,這是可用的時間。 – Jon

回答

1

如果保存出發,按照腳本編輯器中止打開的應用程序,當你運行應用程序,它會創建一個新的錄音,並保持檢查,看看是否有你手動停止錄製。一旦你停止錄製,它將導出文件,打開它並退出。如果您確實希望將其作爲Automator操作,只需使用Automator操作即可打開腳本應用程序。

property savePath : "Desktop:" --update e.g.,: "Dropbox:Inbox:- Voice Memos:" 
property defaultName : "AudioNote" 

my start_recording() 

on start_recording() 
    tell application "QuickTime Player" 
     activate 
     tell (new audio recording) to start 
    end tell 
end start_recording 

on export_recording() 
    set homeFolder to (path to home folder) as string 
    set dateTimeStamp to do shell script "date '+%m%d%y-%H%M%S'" 
    set audioNotePath to (homeFolder & savePath & defaultName & " " & dateTimeStamp & ".m4a") 
    tell application "QuickTime Player" 
     tell document 1 
      export in file audioNotePath using settings preset "Audio Only" 
      close saving no 
     end tell 
    end tell 
    return audioNotePath 
end export_recording 

on open_recording(audioNotePath) 
    tell application "QuickTime Player" --you could change this if you wanted to open the file in another app 
     repeat --keep trying until the file is exported and ready to be opened 
      try 
       delay 1 
       open file audioNotePath 
       exit repeat 
      end try 
     end repeat 
    end tell 
end open_recording 

on idle 
    try 
     tell application "QuickTime Player" to set documentName to name of document 1 
     if (documentName ≠ "Audio Recording") then 
      set audioNotePath to my export_recording() 
      my open_recording(audioNotePath) 
      quit 
     end if 
    end try 
    return 1 
end idle 
+0

非常感謝!這幫助我完成了我想完成的任務。 – Winterflags

相關問題