Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
share|improve this question

3 Answers 3

up vote 5 down vote accepted

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
share|improve this answer
    
+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 :) –  Yar Apr 5 '11 at 1:04
    
@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 Apr 5 '11 at 6:02
    
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? –  Yar Apr 5 '11 at 17:42
    
@Yar Don't know what you mean. There's no way to get a tab's index, and active 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 Apr 5 '11 at 18:24
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 Apr 6 '11 at 5:43

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.

share|improve this answer
    
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!: superuser.com/questions/237758/…. 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 Apr 10 '11 at 8:20

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.