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