我正在编写一个AppleScript,它将以一种类似应用程序的方式启动Netflix。它检查Netflix是否在任何选项卡中打开,如果是,它将“激活”该窗口(关注它),然后将其设置为Netflix选项卡。如果Netflix没有打开,它将在一个新窗口中打开它。
我收到了一个奇怪的错误:"Apple事件处理程序失败“。是什么导致了这个错误?
以下是完整的代码:
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总是在计算一个额外的窗口。为什么会发生这种情况,如何解决呢?
发布于 2011-11-21 14:47:37
退出重复是从制表符循环中分离出来的,而不是窗口循环。所以我不认为这是在做你认为应该做的事情。我建议将逻辑改为while循环,而不是for循环,或者只在整个内循环周围添加exit子句。
repeat with j from 1 to (count of (tabs of window i))
if not netflixIsOpen then
....这样,即使循环继续,它也不会调用两次代码。
https://stackoverflow.com/questions/8213163
复制相似问题