Applescript - 谷歌浏览器激活某个窗口 [英] Applescript - Google Chrome activate a certain window

查看:45
本文介绍了Applescript - 谷歌浏览器激活某个窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Do you guys know how to activate the n-th Google Chrome window with Applescript? For example, if I have three Google Chrome windows opened, how do activate the second?

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

I've tried many different combinations, e.g.: tell tab 3 of window 2 to activate

我也注意到:

告诉应用程序谷歌浏览器"告诉窗口 1 激活最后告诉

tell application "Google Chrome" tell window 1 to activate end tell

和,

告诉应用程序谷歌浏览器"告诉窗口 2 激活最后告诉

tell application "Google Chrome" tell window 2 to activate end tell

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

produce the same results, it only activates the last window opened, Is this a bug?

提前致谢:)

推荐答案

tl;dr

使用设置窗口索引;to 1 并不完全有效,因为它并没有真正激活窗口——不过它确实让它可见.

Using set index of window <n> to 1 isn't fully effective in that it doesn't truly activate the window - it does make it visible, though.

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

Workarounds (examples assume you want to activate window 2):

  • Yar 的回答 提供了一种务实的解决方法,但尚不完全清楚为什么有用.但是,它的优点是要求调用应用程序授权辅助访问,与以下解决方案不同.

  • 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 的回答暗示了一个强大且通用的解决方案,该解决方案也适用于非 AppleScriptable 应用程序:>

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

  • 或者,使用如下定义的 AppleScript 处理程序:

  • Alternatively, use the AppleScript handler defined below as follows:

    • 将应用程序Google Chrome"告诉我的 activateWin(it, window 2)

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

    While adayzdone's answer should work and almost does, there is a catch - which may or may not be a problem (observed on Chrome 21.0.1180.89 on Mountain Lion) [update: still applies as of Chrome 47.0.2526.106 on OSX 10.11.2]:

    虽然该解决方案将显示所需的窗口作为前窗,但如果不同的窗口之前处于活动状态,Chrome 不会将其处理作为前窗.您可以通过不活动的关闭/最小/缩放标题栏按钮、复选标记旁边的窗口标题以及 Cmd-L 等键盘快捷键不适用于所需的窗口.

    While the solution will show the desired window as the front window, Chrome will NOT treat it as the front window if a different window was previously active. You can tell by the inactive close/min/zoom title-bar buttons, by what window title the checkmark is next to, and by the fact that keyboard shortcuts such as Cmd-L will not apply to the desired window.

    如果您的下一步操作是点击窗口上的某处,这可能不是问题,因为这样的点击将完全激活所需的窗口.

    If your next action will be to click somewhere on the window anyway, this may not be a problem, as such a click will fully activate the desired window.

    否则,您可以采用相当健壮的 GUI 脚本解决方法(感激地改编自通用解决方案 here):

    Otherwise, you can employ a reasonably robust GUI-scripting workaround (gratefully adapted from a generic solution here):

    更新:遗憾的是,实际上没有激活索引设置为 1 的窗口的问题似乎影响了所有应用程序(在 OS X 10.8.3 上体验过).这是一个通用函数,它使用 GUI 脚本正确激活给定 AppleScriptable 应用程序中的给定窗口.

    Update: Sadly, the problem of not actually activating the window whose index you set to 1 seems to affect ALL apps (experienced on OS X 10.8.3). Here's a generic function that properly activates a given window in a given AppleScriptable app using GUI scripting.

    # 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 上的所有应用程序中似乎也被破坏 - 而底层应用程序 被激活,窗口规范.被忽略.

    On a side note, what the OP tried - e.g., activate window 1 - seems to be broken in ALL apps on OS X 10.8.3 as well - while the underlying application is activated, the window spec. is ignored.

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

    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
    

    这篇关于Applescript - 谷歌浏览器激活某个窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆