2012-09-19 235 views
1

我有一個腳本比登錄,請問用戶,如果他們想看電影。大多數情況下,它的效果很好。但有時和我不明的原因,我得到一個AppleEvent處理程序失敗的錯誤。我讀過關於這個錯誤的其他帖子,但他們似乎都是獨一無二的。所以,如果可能的話,有人可以看看我的劇本,並告訴我爲什麼偶爾會彈出,如果有什麼我可以做,以防止它?AppleScript - AppleEvent處理程序失敗

有一件事可能有助於知道腳本中發生此錯誤時失敗的一件事是電影無法播放。它在quicktime打開,但不啓動。

在此先感謝,下面是腳本。

tell application "Welcome" to activate 
set question to display dialog "Would you like a welcome video?" buttons {"No, I've seen it", "Yes, please"} default button 2 
set answer to button returned of question 
if answer is equal to "Yes, please" then tell application "QuickTime Player" 
    set theMovie to "Macintosh HD:Library:Desktop Pictures:Mac ML Opening Chalkbaord Video.mov" 
    set openMovie to open theMovie 
    present openMovie 
    play openMovie 
    delay 30 
    quit 
end tell 
if answer is equal to "No, I've seen it" then tell application "Welcome" 
    quit 
    tell application "System Events" 
     delete login item "Welcome" 
    end tell 
end tell 
+0

有什麼場合?一些背景可以走很長的路。準備好編輯你的問題,因爲你可以更深入地瞭解上下文和條件。 –

回答

1

我的猜測是,你可能需要延遲打開和播放電影。有時代碼運行速度比電腦反應速度快。如果是這種情況,那麼當代碼告訴電影播放時,電影可能仍然試圖打開...因此錯誤。因此,我添加了2個重複循環來檢查事情,以確保它們在繼續執行代碼中的下一步之前可用。您還需要在代碼中「打開文件」,而不是「打開」。

你的方法在你的if語句中告訴應用程序做某事是不尋常的。我不會那樣做。我還會將你的if語句合併成一個if/else if語句。無論如何,這裏是我將如何寫你的代碼(我假設應用程序「歡迎」是代碼本身)。我希望這有幫助!

set theMovie to "Macintosh HD:Library:Desktop Pictures:Mac ML Opening Chalkbaord Video.mov" 

tell me to activate 
set question to display dialog "Would you like a welcome video?" buttons {"No, I've seen it", "Yes, please"} default button 2 
set answer to button returned of question 

if answer is equal to "Yes, please" then 
    tell application "QuickTime Player" 
     activate 
     set openMovie to open file theMovie 

     -- delay until the movie opens 
     set startTime to current date 
     repeat until exists document 1 
      delay 0.2 
      if (current date) - startTime is greater than 10 then return -- a precaution so you don't get stuck in the repeat loop forever 
     end repeat 

     present openMovie 
     play openMovie 

     -- delay until the movie stops playing 
     repeat until document 1 is not playing 
      delay 1 
     end repeat 

     quit 
    end tell 
else if answer is equal to "No, I've seen it" then 
    tell application "System Events" to delete login item "Welcome" 
end if 
+0

工作絕對精彩!我希望我能給你買一杯咖啡或啤酒!正如你在我的劇本中注意到的那樣,我知道這足以成爲危險的,因此是不尋常的「if」聲明。謝謝謝謝! – Chuck