Applescript - 激活Google Chrome中的特定窗口

13

你们知道如何使用Applescript激活第n个Google Chrome窗口吗?例如,如果我打开了三个Google Chrome窗口,如何激活第二个窗口?

我已经尝试了许多不同的组合,例如: 告诉第2个窗口的第3个选项卡激活

我还注意到:

告诉应用程序"Google Chrome" 告诉第1个窗口激活 end tell

和,

告诉应用程序"Google Chrome" 告诉第2个窗口激活 end tell

产生相同的结果,只会激活最后打开的窗口,这是一个错误吗?

提前感谢 :)

6个回答

17

简述:

使用set index of window <n> to 1并不能完全有效地激活窗口,它只是使其可见。

解决方法(以下示例假设您想要激活第二个窗口):

  • Yar's answer offers a pragmatic workaround, though it's not entirely clear why it works. It does, however, have the advantage of not requiring the calling application to be authorized for assistive access, unlike the following solutions.

  • user495470's answer hints at a robust and generic solution that also works with non-AppleScriptable applications:

    tell application "System Events" to tell process "Google Chrome"
        perform action "AXRaise" of window 2
        set frontmost to true
    end tell
    
  • Alternatively, use the AppleScript handler defined below as follows:

    • tell application "Google Chrome" to my activateWin(it, window 2)

虽然 adayzdone的答案 应该 可以工作并且 几乎 能够实现,但是有一个问题 - 这可能是一个问题也可能不是(在Mountain Lion上Chrome 21.0.1180.89上观察到)[更新:截至OSX 10.11.2上的Chrome 47.0.2526.106仍然适用]:

尽管这个解决方案会将所需窗口 显示 为前置窗口,但是如果之前激活了其他窗口,Chrome将不会将其视为前置窗口。您可以根据非活动关闭/最小化/缩放标题栏按钮、所选窗口标题旁边的勾号以及键盘快捷键(如Cmd-L)是否应用于所需窗口来判断。

如果您接下来的操作将在窗口上 点击 某个位置,那么这可能不是一个问题,因为这样的单击将完全激活所需窗口。

否则,您可以采用相当强大的GUI脚本解决方法(感激地从通用解决方案here中改编):
更新:遗憾的是,将索引设置为1的窗口实际上未被激活的问题似乎影响所有应用程序(在OS X 10.8.3上经历过)。 这是一个通用函数,使用GUI脚本正确激活给定的AppleScriptable应用程序中的给定窗口。
# Activates the specified window (w) of the specified AppleScriptable
# application (a).
    # Note that both parameters must be *objects*.
# Example: Activate the window that is currently the 2nd window in Chrome:
#   tell application "Google Chrome"
#       my activateWin(it, window 2)
#   end tell
on activateWin(a, w)
    tell application "System Events"
        click menu item (name of w) of menu 1 of menu bar item -2 ¬
                   of menu bar 1 of process (name of a)
    end tell
    activate a
end activateWin

在旁边说一句,OP尝试的 - 例如,activate window 1 - 在OS X 10.8.3上所有应用程序中似乎都存在问题 - 虽然激活了底层的应用程序,但窗口规范被忽略了。
这是原始的、更加教学性的代码:
tell application "Google Chrome"
    # Each window is represented by the title of its active tab
    # in the "Window" menu.
    # We use GUI scripting to select the matching "Window" menu item
    # and thereby properly activate the window of interest.
    # NOTE: Should there be another window with the exact same title,
    # the wrong window could be activated.

    set winOfInterest to window 2 # example
    set winTitle to name of winOfInterest

    tell application "System Events"
        # Note that we must target the *process* here.
        tell process "Google Chrome"
            # The app's menu bar.
            tell menu bar 1
                # To avoid localization issues,
                # we target the "Window" menu by position - the next-to-last
                # rather than by name.
                click menu item winTitle of menu 1 of menu bar item -2
            end tell
        end tell
    end tell
    # Finally, activate the app.
    activate
end tell

8

尝试:

tell application "Google Chrome"
    activate
    set index of window 1 to 1
    delay 3
    set index of window 2 to 1
end tell

哇...这个完美无缺地运行。我以为这是不可能做到的 :) - m41n1

4
正如mklement所提到的,更改索引会将窗口提升,但例如键盘快捷键仍由先前最前面的窗口注册。
tell application "Google Chrome"
    set index of window 2 to 1
end tell

另一种选择是告诉系统事件来提升窗口1:
tell application "Google Chrome"
    set index of window 2 to 1
end tell
tell application "System Events" to tell process "Google Chrome"
    perform action "AXRaise" of window 1
end tell

做得好 - AXRaise确实是一个有效的解决方案; 然而,你可以并且应该单独使用它:tell application "System Events" to tell process "Google Chrome"¬ perform action "AXRaise" of window 2¬ set frontmost to true¬ end tell¬就足够了。 - mklement0

2
除了激活所需的窗口的解决方案外,(至少)新的Chrome版本还支持窗口的active tab index属性:
tell window 1
    set active tab index to 5
end tell

2

设置索引,然后打开Chrome浏览器。

tell application "Google Chrome"
    set index of window 2 to 1
    delay 0.01
    do shell script "open -a Google\\ Chrome"
end tell

Source


+1;有趣的是,这确实有效 - 不像使用看似等效的“激活它”(或尝试将进程对象的“frontmost”属性设置为“true”)- 你能解释为什么吗? - mklement0
1
@mklement0 我无法以令人满意的方式解释。你可以看到,没有 shell 脚本时,窗口焦点确实会改变,但不会改变第一响应者焦点(即文本光标)...再次运行 Chrome 强制它将最前面的窗口设为焦点。这只是解释了发生了什么,而不是如何发生的,但希望能有所帮助。 - Dan Rosenstark

0

如果你运行这个

activate application "Google Chrome"
tell application "System Events" to keystroke "`" using command down

即使从脚本编辑器运行,它也会完成整个操作。

这里还有另一种方法,这个方法已经被其他人详细说明了,只是步骤更多:

tell application "System Events" to tell process "Google Chrome" to perform action "AXRaise" of window 2
activate application "Google Chrome"

如果你愿意将它保存为一个应用程序并分配给一个热键,你可以这样做:
tell application "Google Chrome" to set index of window 2 to 1

虽然我想在那个时候你可能会直接按下command+`。除非当然,你的AppleScript涉及一系列步骤创建一系列步骤,这很可能是情况。如果它涉及一系列步骤,像第二个代码示例的第一行就足够了,只要将其分配给热键,并在Chrome已经激活(或该行出现在您的代码之后)时按下热键。


正如我在答案中所述,虽然这将“显示”指定的窗口,但它不会使其成为“活动”窗口(就输入/键盘焦点而言)。 - mklement0
不,这并不是重复的内容,尽管有人提到过“将窗口2的索引设置为1”,但他们的解释有3行,而我的只有1行。如果你想让它聚焦,似乎必须再加一行,但仍然只有2行。这很重要,也不会冗余,因为如果你不知道可以将其缩减为一行,你可能会觉得这种语言太奇怪了,不值得去理解。另外,我现在编辑了原始内容,现在它可以聚焦了。 - pau
如果你认为将一个现有答案中的三行变成一行会增加价值,那么你应该在你的回答中明确说明。 话虽如此,你后来添加的那一行解决方案是不可行的: “激活应用程序”语句会重新激活先前处于活动状态的窗口。 - mklement0
我加入了自己的想法。我猜它并不比终端命令更好。实际上,可以说更糟糕,因为你要去到更高的索引,就需要多次执行按键操作。可能最好采用其中一种方法,只需指示某个特定的索引,然后咬紧牙关,做必要的反向操作,以使窗口在索引1处获得焦点。好的一面是,这确实会转到索引2处的窗口。 - pau

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接