Determining the Active Tab in Chrome/Firefox using AutoHotkey

One of the limitations of using AutoHotkey to control Google Chrome comes in the form of determining the active tab. I have seen this brought up in a number of different places online and have seen some clever means of accomplishing this goal, including looping through all the different tabs and determining the names of each before acting on any of them. Though this would admittedly provide more control, it strikes me as heavy-handed and inelegant.

If your only goal is to determine the active tab (so you can, for example, perform actions elsewhere before reactivating the tab), a much simpler solution is to use the PixelSearch function. Assuming you are not using some awful theme wherein the active tab color is completely indistinguishable from the inactive tabs, it’s just a matter of searching along the tab bar for the appropriate color and storing its physical location on the screen for later use. From there, you can open a new tab, or navigate to some other tab using simple keyboard shortcuts (like Ctrl+1 through 9, for instance) and then return to where you were using that information.

For example, I use a web application called SubSonic to stream my music, and I wanted to add keyboard shortcuts to control the Flash-based player open in a pinned tab in Chrome. By simply using #IfWinActive I was able to determine whether or not I was already on the SubSonic tab in Chrome. If I found that I was not, I determined the X and Y coordinates of the tab I was presently on, used Ctrl+1 to activate the SubSonic tab (knowing that I always kept it pinned as the first tab on the left), ran my macros and then navigated back to my last tab with a click. Excluding the various macros for the SubSonic stuff, the script was as simple as:

PixelSearch, Xtab, Ytab, 45,26,1771,26,0xFBFBFC,0,Fast ; Determines the active tab

Xtab += 25 ; The first white pixel will be to the left of where you’ll want to send your click later.

Send ^1 ; Activate my pinned SubSonic tab

Sleep 50 ; Don’t wanna move too fast!

MyMethod() ;  My macro went here…

Click, %Xtab%, %Ytab% ; Return me to the tab I was on in the first place.

It is not the prettiest thing in the world, but until there is some more direct means of interacting with tabs in Chrome, it’ll do the trick.

 

Leave a Reply

Your email address will not be published. Required fields are marked *