5
4
I was hoping that the following code would do the job, but no cigar:
--Only the window is brought into focus
tell application "Google Chrome"
activate tab 1 of window 1
end tell
5
4
I was hoping that the following code would do the job, but no cigar:
--Only the window is brought into focus
tell application "Google Chrome"
activate tab 1 of window 1
end tell
10
Google Chrome is, in fact, scriptable.
tell application "Google Chrome" to set active tab index of first window to 3
Works like a charm for version 10.0.648.204.
While it would be nice to do something like the following:
tell application "Google Chrome" to set active tab of first window
to first tab of the first window whose title is "Super User"
It's not possible, since active tab
is a read-only property. You'd need to loop over all a window's tabs to find the index of the one you want by querying each tab's title, and then set the active tab index
:
tell application "Google Chrome"
set i to 0
repeat with t in (tabs of (first window whose index is 1))
set i to i + 1
if title of t is "Super User" then
set (active tab index of (first window whose index is 1)) to i
end if
end repeat
end tell
4
I just finished this amazing script, which required a lot of googling and guessing, but it works.
tell application "Google Chrome"
activate
repeat with w in (windows)
set j to 0
repeat with t in (tabs of w)
set j to j + 1
if title of t contains "Workflowy" then
set (active tab index of w) to j
set index of w to 1
tell application "System Events" to tell process "Google Chrome"
perform action "AXRaise" of window 1 -- `set index` doesn't always raise the window
end tell
return
end if
end repeat
end repeat
end tell
The do shell script
is from here: it gets the Window to accept keystrokes.
You can use FastScripts to make this work (and there are many other methods, too)
This works very well. And thankfully with Google Chrome, having duplicate pages in the browser does not cause an error (unlike Safari). – I0_ol – 2017-05-20T08:03:24.370
After using this a bit, I noticed the i
variable doesn't seem to be doing anything. Unless I'm missing something, the script seems to work the same without it. – I0_ol – 2017-06-21T23:03:37.080
Also, it would probably be more informative to have more descriptive variable names. Something like the_window
for w
and the_tab
for t
and tab_index
for j
would probably go a long way in helping others better understand what is going on. – I0_ol – 2017-06-21T23:15:59.873
1
I was trying to write a script to pause and play Netflix, and the code provided above was a good framework for searching through the tabs. "whose index is 1" kept throwing compiler errors on my Mac Mini 10.8.3, so, based on code from http://en.blog.guylhem.net/post/9835498027/google-chrome-next-tab-in-applescript , I just removed the reference entirely (which worked for my purposes)
The script basically activates the browser window, goes through tabs until it finds one titled "Netflix", and sends it key code 49 (spacebar).
tell application "Google Chrome"
activate
set i to 0
repeat with t in (tabs of (first window))
set i to i + 1
if title of t is "Netflix" then
set (active tab index of (first window)) to i
end if
end repeat
tell application "System Events" to key code 49
end tell
0
Google Chrome isn't scriptable (that is, it doesn't understand Apple Events). You can script it the hard way using the Accessibility API (which you need to turn on in System Preferences > System > Universal Access; see the "Enable access for assistive devices" checkbox at the bottom); this is done via the Processes suite of System Events. Unfortunately, this is extremely painful in the general case, but something like
tell application "System Events"
tell application process "Google Chrome"
click tab 1 of window 1
end tell
end tell
might suffice. If it doesn't, then you'll need to explicitly dig the tab element out of the widget tree; Apple has some sample code that can help you find UI elements.
Although I didn't find it applicable for this question I found this info. to be quite useful. It almost helped me figure out the answer to this question!: http://superuser.com/questions/237758/using-applescript-to-enable-snow-leopards-text-replacement-feature. Unfortunately, in that case the AppleScript Editor seemed to refuse to acknowledge the existence of the scroll area containing my desired UI element that UIElement Inspector told me to look for.
– Orion751 – 2011-04-10T08:20:55.840
This no longer works. Now it returns an error.
Google Chrome got an error: Can’t get window 1 whose index = 1. Invalid index.
– I0_ol – 2017-05-20T07:31:31.980+1... "You'd need to loop over all a window's tabs..." yep, that's called programming... to not be embarrassed to write long, ugly code as long as there's no other way through it :) – Dan Rosenstark – 2011-04-05T01:04:09.170
@Yar It is inconvenient though, especially since AppleScript apparently tries to be usable by people who are not programmers. But since the user didn't want that anyway, I didn't write it ;-) – Daniel Beck – 2011-04-05T06:02:56.067
I don't actually know Applescript, but if this part is valid
whose title is "Super User"
then I'd think that you could get the tab index directly from, um, the "first window"... no way? – Dan Rosenstark – 2011-04-05T17:42:30.343@Yar Don't know what you mean. There's no way to get a tab's
index
, andactive tab
is read-only. The first snippet works, which is what the user wanted. I added to my post how you'd do the second example so it works. – Daniel Beck – 2011-04-05T18:24:02.210@Daniel Beck, understood, thanks. It's surprising that these basic things are not accounted for. Is this the API for Chrome (?) or Applescript that is to blame (so to speak)? – Dan Rosenstark – 2011-04-06T02:44:20.637
1@Yar This particular issue is the API provided by the Chrome devs. It might be related to that being a major pain to do, but I have never had to implement AppleScript support myself, so I'm not qualified to judge. – Daniel Beck – 2011-04-06T05:43:04.463
@Daniel Beck Sorry for my failure to respond sooner. The code you mentioned indeed works. Now I've got to deal with it sometimes failing to create a new window for the tabs when I ask it to, but that's probably a bug on their end. – Orion751 – 2011-04-10T08:02:01.370
You can actually change to a tab by its name in a single event descriptor (at least in 14.0):
tell app "Google Chrome" to tell window 1 to set active tab index to id of tab 1 where title is "Google"
– Lri – 2011-11-24T11:24:55.867