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 1isn’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:
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-Lwill 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.
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: