我有一个脚本而不是登录,询问用户是否要观看电影。在大多数情况下,它的效果很好。但有时候,由于我不知道的原因,我得到一个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