首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AppleScript for Netflix:失败

AppleScript for Netflix:失败
EN

Stack Overflow用户
提问于 2011-11-21 14:14:48
回答 1查看 1.1K关注 0票数 2

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

我收到了一个奇怪的错误:"Apple事件处理程序失败“。是什么导致了这个错误?

以下是完整的代码:

代码语言:javascript
复制
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的选项卡)计数)”。

下面是调试:

代码语言:javascript
复制
    count every tab of window 2
        --> error number -10000
Result:
error "Safari got an error: AppleEvent handler failed." number -10000

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

EN

回答 1

Stack Overflow用户

发布于 2011-11-21 14:47:37

退出重复是从制表符循环中分离出来的,而不是窗口循环。所以我不认为这是在做你认为应该做的事情。我建议将逻辑改为while循环,而不是for循环,或者只在整个内循环周围添加exit子句。

代码语言:javascript
复制
repeat with j from 1 to (count of (tabs of window i))
   if not netflixIsOpen then
     ....

这样,即使循环继续,它也不会调用两次代码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8213163

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档