【问题标题】:Applescript - Google Chrome activate a certain windowApplescript - Google Chrome 激活某个窗口
【发布时间】:2012-05-09 02:37:03
【问题描述】:

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

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

我还注意到:

告诉应用程序“谷歌浏览器” 告诉窗口 1 激活 结束告诉

和,

告诉应用程序“谷歌浏览器” 告诉窗口 2 激活 结束告诉

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

提前致谢:)

【问题讨论】:

    标签: google-chrome applescript


    【解决方案1】:

    tl;dr

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

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

    • Yar's answer 提供了一种实用的解决方法,尽管它的工作原理并不完全清楚为什么。但是,它确实具有要求调用应用程序为authorized for assistive access 的优点,这与以下解决方案不同。

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

      tell application "System Events" to tell process "Google Chrome"
          perform action "AXRaise" of window 2
          set frontmost to true
      end tell
      
    • 或者,使用如下定义的 AppleScript 处理程序:

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

    虽然adayzdone's answer 应该工作并且几乎工作,但有一个问题 - 这可能是也可能不是问题(在 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
    

    【讨论】:

      【解决方案2】:

      试试:

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

      【讨论】:

      • 哇......这完美无瑕。我认为这是不可能的:)
      【解决方案3】:

      正如 mklement 提到的,更改索引会提升窗口,但例如键盘快捷键仍由先前最前面的窗口注册。

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

      另一种选择是将系统事件告诉 AXRaise 窗口 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¬ 就足够了。
      【解决方案4】:

      设置索引,然后打开 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;奇怪的是,这确实有效——不像使用看似等效的activate it(或试图将进程对象的frontmost属性设置为true)——你能解释一下为什么吗?
      • @mklement0 我无法以令人满意的方式解释它。您可以看到,如果没有 shell 脚本,窗口焦点确实会改变,但 firstResponder 焦点(基本上是文本光标)不会改变……再次运行 Chrome 会强制它为其最前面的窗口提供焦点。所以这只是解释正在发生的事情,而不是如何发生的,但希望它会有所帮助。
      【解决方案5】:

      除了激活所需窗口的解决方案外,(至少)较新的 Chrome 版本还支持 windows 的 active tab index 属性:

      tell window 1
          set active tab index to 5
      end tell
      

      【讨论】:

        【解决方案6】:

        如果你运行这个

        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
        

        虽然我想你也可以简单地点击命令+`。当然,除非您的 applescript 涉及创建一系列步骤的一系列步骤,这很可能是这种情况。如果它涉及一系列步骤,那么您只需要第二个代码示例的第一行,只要您将它分配给一个热键并在 chrome 已经激活时点击热键(或者该行出现在 chrome 之后在您的代码中激活)。

        【讨论】:

        • 正如我在回答中所说,虽然这将 显示 指定窗口,但它不会使其成为 活动 窗口(在输入/键盘焦点)。
        • 不,这不是重复,虽然其他人确实提到了“将窗口 2 的索引设置为 1”,他们的解释是 3 行,我的是 1。如果你想让它聚焦,它似乎必须是一个额外的行,但仍然只有 2 行。它很重要而且不是多余的,因为如果你不知道你可以把它缩小到一行,你可能会觉得这种语言太奇怪了,不值得尝试去掌握。我也编辑了我原来的东西,现在它集中了。
        • 如果您真的相信将 3 行从 现有答案 变为 1 行会增加价值,那么您应该在答案中说明。也就是说,您使用稍后添加的附加行的解决方案尝试不起作用:activate application 语句重新激活以前的活动窗口。
        • 我添加了我自己的看法。我想它并不比终端命令方法好多少。事实上,它可以说更糟,因为你想去的索引越高,你必须做的击键线就越多。最好使用一种方法,让您在某处指示特定索引,然后咬紧牙关,做任何你必须做的事情,以使索引 1 处的窗口真正聚焦。从好的方面来说,这转到索引 2 处的窗口。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-16
        • 1970-01-01
        相关资源
        最近更新 更多