AppleScript for Netflix:Apple事件处理程序失败

时间:2011-11-21 14:14:49

标签: applescript

我正在编写一个以类似应用程序的方式启动Netflix的AppleScript。它检查Netflix是否在任何选项卡中打开,如果是,它将“激活”该窗口(关注它),然后将其设置为Netflix选项卡。如果Netflix未打开,它将在新窗口中打开。

我收到一个奇怪的错误:“Apple Event Handler失败”。导致此错误的原因是什么?

以下是完整代码:

set netflixIsOpen to false
tell application "Safari"
    repeat with i from 1 to (count of windows)
        repeat with j from 1 to (count of (tabs of window i))
            set theURL to URL of tab j of window i
            if theURL contains ".netflix.com/" then
                -- display dialog "netflix found!"
                set netflixIsOpen to true
                set miniaturized of window i to false
                activate window i
                tell window i
                    set current tab to tab j
                    -- if not logged in, then go to the homepage
                    if theURL does not contain "movies.netflix.com/" then
                        set URL of tab j to "http://movies.netflix.com/WiHome"
                    end if
                end tell
                exit repeat
            end if
        end repeat
    end repeat
    if not netflixIsOpen then
        make new document with properties {URL:"http://movies.netflix.com/WiHome"}
    end if
end tell

编辑:错误集中在“((窗口i的标签)的数量)”。

这是调试:

    count every tab of window 2
        --> error number -10000
Result:
error "Safari got an error: AppleEvent handler failed." number -10000

只有一个窗口打开。似乎AppleScript总是在计算一个额外的窗口。为什么会发生这种情况?如何解决?

1 个答案:

答案 0 :(得分:0)

您的退出重复是跳出选项卡循环,但不是窗口循环。所以我认为这不符合你的想法。我建议将逻辑更改为while循环而不是for循环,或者只是在整个内循环中添加exit子句

repeat with j from 1 to (count of (tabs of window i))
   if not netflixIsOpen then
     ....

就像那样,即使循环将继续,它也不会两次调用代码。