Applescript - 谷歌浏览器激活某个窗口
你们知道如何使用Applescript激活第n个谷歌浏览器窗口吗?例如,如果我打开了三个Google Chrome窗口,那么如何激活第二个?Applescript - 谷歌浏览器激活某个窗口
我已经尝试了许多不同的组合,如:窗口2 告诉标签3激活
我也注意到:
告诉应用程序“谷歌浏览器” 告诉视窗1激活 端告诉
,并
告诉应用程序 “谷歌浏览器” 告诉窗口2激活 结束告诉
产生相同的结果,它只激活最后一个窗口 打开,这是一个错误吗?
感谢提前:)
TL;博士
使用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应该工作和几乎呢,有一个陷阱 - 这可能会或可能不会是一个问题(在Chrome 21.0.1180.89观察山狮)更新:仍然适用,适用于Chrome在OSX 10.11上为47.0.2526.106。2]:
虽然该解决方案将显示期望窗口作为前窗,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
尝试:
tell application "Google Chrome"
activate
set index of window 1 to 1
delay 3
set index of window 2 to 1
end tell
正如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”确实是一个有效的解决方案;但是,您可以并且应该使用它_by本身_:'告诉应用程序“系统事件”来告诉进程“Google Chrome”¬ \t执行窗口2的操作“AXRaise” \t将最前面设置为true end tell是'足够。 – mklement0 2015-12-21 04:50:31
设置索引,然后打开Chrome。
tell application "Google Chrome"
set index of window 2 to 1
delay 0.01
do shell script "open -a Google\\ Chrome"
end tell
+1;奇怪的是,这个_does_工作 - 不像使用看起来相同的'activate it'(或者试图将过程对象的'frontmost'属性设置为'true') - 你能解释为什么吗? – mklement0 2015-12-21 04:58:59
@ mklement0我无法用令人满意的方式解释它。你可以看到,如果没有shell脚本,窗口焦点会改变,但不会改变firstResponder焦点(文本光标,基本上)......再次运行Chrome会强制它把焦点放在最前面的窗口上。所以这只是解释发生了什么,但不是如何,但希望它有帮助。 – 2015-12-21 20:59:27
如果运行此
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"
如果youre愿意将它保存为一个应用程序并将其分配给一个热键,你可以这样做:
tell application "Google Chrome" to set index of window 2 to 1
虽然我想在这一点上,你可能只是简单地命令+。除非当然,否则你的applescript会涉及一系列步骤,创建一系列步骤,情况可能如此。如果涉及一系列步骤,则只需将其分配给热键并在Chrome已激活时点击热键即可(例如第二个代码示例的第一行),或者在Chrome之后在你的代码中激活)。
正如我在我的回答中所述,虽然这将显示指定的窗口,但它不会使其成为_active_(根据输入/键盘焦点)。 – mklement0 2015-12-21 04:29:03
不,这不是重复,尽管其他人提到了“将窗口2的索引设置为1”,他们的解释是3行,我的是1.如果你想要它的重点,它似乎必须是一个额外的线,但仍然只有2行。它的重要而不是多余的,因为如果你不知道你可以缩小到一行,你可能会觉得这种语言太奇怪了,不值得试图去掌握。我也编辑了我原来的东西,现在它的重点。 – pau 2015-12-21 04:47:16
如果您确实认为将_existing answer_中的3行转换为1会增加值,则应在您的答案中指出_。也就是说,您的解决方案尝试使用稍后添加的附加行是行不通的:'activate application'语句重新激活之前活动的窗口。 – mklement0 2015-12-21 04:53:53
除了关于激活所需窗口的解决方案,(至少)新版Chrome还支持Windows的active tab index
属性:
tell window 1
set active tab index to 5
end tell
哇......这个完美的作品。我认为这是不可能的:) – m41n1 2012-04-28 23:02:55