1

我有一个脚本,而不是登录,询问用户是否想看电影。在大多数情况下,它工作得很好。但是,有时出于我未知的原因,我会收到 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
4

1 回答 1

2

我的猜测是,您可能需要在打开和播放电影之间进行延迟。有时代码运行的速度比计算机的反应速度还要快。如果是这种情况,那么当代码告诉电影播放时,电影可能仍在尝试打开......因此出现错误。因此,我添加了 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
于 2012-09-19T21:25:52.540 回答