Applescript - Google Chrome激活某个窗口

m41*_*1n1 12 applescript google-chrome

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

我尝试了很多不同的组合,例如:告诉窗口2的标签3激活

我也注意到了:

告诉应用程序"Google Chrome"告诉窗口1激活结束告诉

和,

告诉应用程序"Google Chrome"告诉窗口2激活结束告诉

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

提前致谢 :)

mkl*_*nt0 13

TL;博士

使用set index of window <n> to 1不是完全有效的,因为它没有真正激活窗口 - 它确实使它可见.

解决方法(示例假设您要激活窗口2):

  • 亚尔的答案提供了一个务实的解决办法,但它不是完全清楚为什么它的工作原理.但是,与以下解决方案不同,它确实具有要求调用应用程序被授权进行辅助访问的优点.

  • user495470的答案暗示了一个强大而通用的解决方案,该解决方案也适用于非AppleScriptable应用程序:

    tell application "System Events" to tell process "Google Chrome"
        perform action "AXRaise" of window 2
        set frontmost to true
    end tell
    
    Run Code Online (Sandbox Code Playgroud)
  • 或者,使用下面定义的AppleScript处理程序,如下所示:

    • 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脚本解决方法(在此处感谢改进的通用解决方案):

更新:遗憾的是,实际上没有激活其索引设置为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
Run Code Online (Sandbox Code Playgroud)

另外,activate window 1在OS X 10.8.3上的所有应用程序中,OP尝试了什么 - 例如,- 似乎也被打破了 - 当底层应用程序被激活时,窗口规范.被忽略了.

这是原始的,更多的教学代码:

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
Run Code Online (Sandbox Code Playgroud)


ada*_*one 8

尝试:

tell application "Google Chrome"
    activate
    set index of window 1 to 1
    delay 3
    set index of window 2 to 1
end tell
Run Code Online (Sandbox Code Playgroud)