你们知道如何使用Applescript激活第n个Google Chrome窗口吗?例如,如果我打开了三个Google Chrome窗口,如何激活第二个窗口?
我已经尝试了许多不同的组合,例如: 告诉第2个窗口的第3个选项卡激活
我还注意到:
告诉应用程序"Google Chrome" 告诉第1个窗口激活 end tell
和,
告诉应用程序"Google Chrome" 告诉第2个窗口激活 end tell
产生相同的结果,只会激活最后打开的窗口,这是一个错误吗?
提前感谢 :)
你们知道如何使用Applescript激活第n个Google Chrome窗口吗?例如,如果我打开了三个Google Chrome窗口,如何激活第二个窗口?
我已经尝试了许多不同的组合,例如: 告诉第2个窗口的第3个选项卡激活
我还注意到:
告诉应用程序"Google Chrome" 告诉第1个窗口激活 end tell
和,
告诉应用程序"Google Chrome" 告诉第2个窗口激活 end tell
产生相同的结果,只会激活最后打开的窗口,这是一个错误吗?
提前感谢 :)
简述:
使用set index of window <n> to 1
并不能完全有效地激活窗口,它只是使其可见。
解决方法(以下示例假设您想要激活第二个窗口):
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)
虽然 adayzdone的答案 应该 可以工作并且 几乎 能够实现,但是有一个问题 - 这可能是一个问题也可能不是(在Mountain Lion上Chrome 21.0.1180.89上观察到)[更新:截至OSX 10.11.2上的Chrome 47.0.2526.106仍然适用]:
尽管这个解决方案会将所需窗口 显示 为前置窗口,但是如果之前激活了其他窗口,Chrome将不会将其视为前置窗口。您可以根据非活动关闭/最小化/缩放标题栏按钮、所选窗口标题旁边的勾号以及键盘快捷键(如Cmd-L
)是否应用于所需窗口来判断。
如果您接下来的操作将在窗口上 点击 某个位置,那么这可能不是一个问题,因为这样的单击将完全激活所需窗口。
否则,您可以采用相当强大的GUI脚本解决方法(感激地从通用解决方案here中改编):# 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
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
tell application "Google Chrome"
set index of window 2 to 1
end tell
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¬
就足够了。 - mklement0active tab index
属性:tell window 1
set active tab index to 5
end tell
设置索引,然后打开Chrome浏览器。
tell application "Google Chrome"
set index of window 2 to 1
delay 0.01
do shell script "open -a Google\\ Chrome"
end tell
如果你运行这个
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
虽然我想在那个时候你可能会直接按下command+`。除非当然,你的AppleScript涉及一系列步骤创建一系列步骤,这很可能是情况。如果它涉及一系列步骤,像第二个代码示例的第一行就足够了,只要将其分配给热键,并在Chrome已经激活(或该行出现在您的代码之后)时按下热键。