【问题标题】:AppleScript - AppleEvent handler failedAppleScript - AppleEvent 处理程序失败
【发布时间】:2012-09-19 16:24:11
【问题描述】:

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

【问题讨论】:

  • 什么场合?一些背景可以走很长的路。随着对上下文和条件的深入了解,准备好编辑您的问题。

标签: applescript quicktime


【解决方案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

【讨论】:

  • 效果非常棒!我希望我能给你买杯咖啡或啤酒!正如您在我的脚本中注意到的那样,我知道这足以造成危险,因此是不寻常的“if”语句。谢谢,谢谢!
猜你喜欢
  • 1970-01-01
  • 2015-11-04
  • 2012-02-09
  • 1970-01-01
  • 2011-10-19
  • 2011-07-15
  • 1970-01-01
  • 1970-01-01
  • 2021-06-05
相关资源
最近更新 更多