2016-06-08 78 views
0

我有一个脚本,除其他外,它记录了它在启动时被激活的浏览器窗口。事情发生在中间,然后脚本需要回到原来的窗口和选项卡上。如何使用AppleScript中的窗口ID将Safari窗口带到前台?

问题是,用户可能会在脚本运行期间更改活动窗口或选项卡。我想返回到调用脚本时使用的窗口和选项卡。

以下是我正在尝试(和失败)要做到这一点:

tell application "Safari" 
    if (id of front window) is not (windowID of browserInfo) 
     display dialog "Made it in the block!" 
     display dialog (get index of window 1) 
     display dialog (get index of window (windowID of browserInfo)) 
-- ... 

的对话框都是为了调试,当然。 现在,browserInfo是一个对象,其windowID属性对应于调用该脚本的Safari窗口。这通常是像'889'或'1195'或其他数字。

现在有趣的是,当模拟在一个窗口中启动的用户,然后激活另一个窗口时,前四条线会正确触发。如预期的那样,第四行返回“1”。但第五行给出了一个错误:Safari got an error: Can't get window 809. Invalid index.

如何才能得到一个Safari窗口的索引,当我可以使用的是一个ID? (可以,URL和窗口标题是很好的东西,但是它们超出了我的应用程序的范围,用户可能会打开多个窗口,并且具有相同的URL和窗口标题,所以我需要特定的窗口标识)。

回答

2

我相信这是你追求的...

on run 
    tell application "Safari" 
     set myID to id of window 1 
     set myTab to current tab of window 1 
    end tell 

    do shell script "sleep 8" -- simulates time where user may change tabs 

    tell application "Safari" 
     set index of window id myID to 1 
     set current tab of window 1 to myTab 
    end tell 
end run 
+0

好极了!我遗漏的一部分是在窗口之后但在变量之前包含'id'。即使试图设置id为(browserInfo的windowID)的窗口的set index也没有切割它。 – Rydash

相关问题