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

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

问题描述

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



我尝试了许多不同的组合,例如:
tell tab 3的窗口2激活



我也注意到:



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



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

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

预先感谢:)

解决方案



使用设置窗口的索引< n>< p>到1 并不完全有效,因为它并不真正激活窗口 - 它确实使窗口可见

>

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


  • 雅尔的回答提供了一个实用的解决方法,尽管它并不完全清楚为什么有用。但是,它确实有的优点,而 不需要调用应用程序授权进行辅助访问,与以下解决方案不同。

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

     告诉应用程序System Events告诉进程Google Chrome
    执行窗口2的操作AXRaise
    将最前面的值设置为true
    结束tell


  • 另外,使用下面定义的AppleScript处理程序,如下所示:


  • 告诉应用程序Google Chrome到我的activateWin(它,窗口2)






ADAY zdone的回答 应该差不多,有一个问题 - 这可能是也可能不是问题(在Mountain Lion的Chrome 21.0.1180.89上观察到) [ update:仍然适用于OSX 10.11.2中的Chrome 47.0.2526.106]:



虽然解决方案将 show 将所需的窗口作为前窗,如果之前有不同的窗口处于活动状态,Chrome将不会将其视为前窗。您可以通过非活动的关闭/分钟/缩放标题栏按钮,旁边的复选标记的窗口标题以及键盘快捷键(如 Cmd-L 不适用于所需的窗口。



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



否则,您可以使用一个相当健壮的GUI脚本编制解决方法(从通用解决方案 here ):

更新:不幸的是,实际上并未激活其索引设置为1的窗口似乎影响了所有应用程序(在OS X上体验过10.8.3)。
这是一个通用函数,它可以使用GUI脚本正确地激活给定AppleScriptable应用程序中的给定窗口。

 #激活指定AppleScriptable 
#应用程序(a)的指定窗口(w)。
#请注意,这两个参数必须是* objects *。
#示例:激活当前Chrome中第二个窗口的窗口:
#告诉应用程序Google Chrome
#我的activateWin(它,窗口2)
#end tell
on activateWin(a,w)
告诉应用程序系统事件
单击菜单栏项目菜单栏菜单项¬
菜单栏的菜单项(w的名称) 1的进程(名称)
结束告诉
激活
结束激活赢

在附注中,OP所尝试的内容 - 例如激活窗口1 - 似乎也在OS X 10.8.3的所有应用程序中被破解 - 而底层的应用程序被激活时,窗口规范。

以下是原始的更多教学代码

 告诉应用程序Google Chrome
#在窗口菜单中,每个窗口都由其活动选项卡
#的标题表示。
#我们使用GUI脚本来选择匹配的窗口菜单项
#,从而正确激活感兴趣的窗口。
#注意:如果有另一个窗口的标题完全相同,
#可能会激活错误的窗口。

将winOfInterest设置为窗口2#示例
将winTitle设置为winOfInterest的名称

告诉应用程序系统事件
#请注意,我们必须针对*进程*在这里。
告诉程序Google Chrome
#应用程序的菜单栏。
告诉菜单栏1
#为了避免本地化问题,我们将
#定位到窗口菜单的位置 - 倒数第二个
#而不是名称。
单击菜单栏项目菜单栏1的菜单项winTitle -2
end tell
end tell
end tell
#最后,激活应用程序。
activate
end tell


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?

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

I've also noticed that:

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

and,

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

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

Thanks in advance :)

解决方案

tl;dr

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.

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

  • 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)

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]:

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.

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

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

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.

Here's the original, more didactic code:

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天全站免登陆