user3411784
user3411784

Reputation: 43

How can I activate specific tabs in Chrome or Firefox with AutoHotkey?

I have 20 different tabs I'd like to be able to open by simply hitting Ctrl+A

The following works for the first 9 tabs, but not beyond that

#SingleInstance, Force
#IfWinActive, ahk_exe chrome.exe
^a::Send, ^2

So I read I'm supposed to use WinActivate

I can get WinActivate to work with programs like notepad, but not with Chrome tabs. Any ideas?

^a::
SetTitleMatchMode, 2
IfWinExist, Twitter ahk_class Chrome_WidgetWin_1
  WinActivate

Upvotes: 4

Views: 6486

Answers (1)

vafylec
vafylec

Reputation: 1015

I have been working on functions to achieve this functionality for a while. I have completed this project just now, and have a script that should achieve the functionality you require.

Note: depending on changes to Firefox and Chrome, these scripts can break at any time, as have previous attempts at manipulating Firefox tabs, however do check the AutoHotkey forums for updates, and/or the following link:

Firefox/Chrome, get tab names/focus tab - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=26947&p=126248#p126248

Note: this script requires the Acc library to run, see the link:

Acc library (MSAA) and AccViewer download links - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=26201

GroupAdd, vGroupFirefoxAndChrome, ahk_class MozillaWindowClass
GroupAdd, vGroupFirefoxAndChrome, ahk_class Chrome_WidgetWin_1

#IfWinActive, ahk_group vGroupFirefoxAndChrome
^a::
^b::
^c::
^d::
^e::
^f::
^g::
^h::
^i::
^j::
^k::
^l::
^m::
^n::
^o::
^p::
^q::
^r::
^s::
^t::
^u::
^v::
^w::
^x::
^y::
^z::
WinGet, hWnd, ID, A
WinGetClass, vWinClass, ahk_id %hWnd%
vLetter := SubStr(A_ThisHotkey, 1-1)
vNum := Asc(vLetter)-96

if (vWinClass = "MozillaWindowClass")
JEE_FirefoxFocusTabByNum(hWnd, vNum)
if (vWinClass = "Chrome_WidgetWin_1")
JEE_ChromeFocusTabByNum(hWnd, vNum)
Return
#IfWinActive

;==================================================

JEE_FirefoxGetTabNames(hWnd, vSep="`n")
{
oAcc := Acc_Get("Object", "4", 0, "ahk_id " hWnd)
Loop, % oAcc.accChildCount
if (oAcc.accName(A_Index) = "Browser tabs")
if (1, oAcc := Acc_Child(oAcc, A_Index))
break
oAcc := Acc_Child(oAcc, 1)

vOutput := ""
Loop, % oAcc.accChildCount
{
vTabText := oAcc.accName(A_Index)
if !(vTabText == "")
;if !(vTabText == "New Tab")
;if !(vTabText == "Open a new tab")
vOutput .= vTabText vSep
}
vOutput := SubStr(vOutput, 1, -StrLen(vSep)) ;trim right

oAcc := ""
Return vOutput
}

;==================================================

JEE_FirefoxFocusTabByNum(hWnd, vNum)
{
oAcc := Acc_Get("Object", "4", 0, "ahk_id " hWnd)
Loop, % oAcc.accChildCount
if (oAcc.accName(A_Index) = "Browser tabs")
if (1, oAcc := Acc_Child(oAcc, A_Index))
break
oAcc := Acc_Child(oAcc, 1)

vRet := 0
For each, oChild in Acc_Children(oAcc)
{
if (A_Index = vNum)
if (1, oChild.accDoDefaultAction(0), vRet := A_Index)
break
}

Return vRet
}

;==================================================

JEE_FirefoxFocusTabByName(hWnd, vTitle, vNum=1)
{
oAcc := Acc_Get("Object", "4", 0, "ahk_id " hWnd)
Loop, % oAcc.accChildCount
if (oAcc.accName(A_Index) = "Browser tabs")
if (1, oAcc := Acc_Child(oAcc, A_Index))
break
oAcc := Acc_Child(oAcc, 1)

vCount := 0
vRet := 0
For each, oChild in Acc_Children(oAcc)
{
vTabText := oChild.accName(0)
if (vTabText = vTitle)
vCount ++
if (vCount = vNum)
if (1, oChild.accDoDefaultAction(0), vRet := A_Index)
break
}

oAcc := ""
Return vRet
}

;==================================================

JEE_ChromeGetTabNames(hWnd, vSep="`n")
{
oAcc := Acc_ObjectFromWindow(hWnd)
oAcc := Acc_Child(oAcc, 1), oAcc := Acc_Child(oAcc, 2)
oAcc := Acc_Child(oAcc, 2), oAcc := Acc_Child(oAcc, 2)

vOutput := ""
For each, oChild in Acc_Children(oAcc)
{
vTabText := Acc_Child(oChild, 1).accName(0)
if !(vTabText == "")
vOutput .= vTabText vSep
}
vOutput := SubStr(vOutput, 1, -StrLen(vSep)) ;trim right

oAcc := ""
Return vOutput
}

;==================================================

JEE_ChromeFocusTabByNum(hWnd, vNum)
{
oAcc := Acc_ObjectFromWindow(hWnd)
oAcc := Acc_Child(oAcc, 1), oAcc := Acc_Child(oAcc, 2)
oAcc := Acc_Child(oAcc, 2), oAcc := Acc_Child(oAcc, 2)

vRet := 0
For each, oChild in Acc_Children(oAcc)
{
if (A_Index = vNum+1)
if (1, oChild.accDoDefaultAction(0), vRet := A_Index)
break
}

Return vRet
}

;==================================================

JEE_ChromeFocusTabByName(hWnd, vTitle, vNum=1)
{
oAcc := Acc_ObjectFromWindow(hWnd)
oAcc := Acc_Child(oAcc, 1), oAcc := Acc_Child(oAcc, 2)
oAcc := Acc_Child(oAcc, 2), oAcc := Acc_Child(oAcc, 2)

vCount := 0
vRet := 0
For each, oChild in Acc_Children(oAcc)
{
vTabText := oChild.accName(0)
if (vTabText = vTitle)
vCount ++
if (vCount = vNum)
if (1, oChild.accDoDefaultAction(0), vRet := A_Index)
break
}

oAcc := ""
Return vRet
}

;==================================================

Upvotes: 2

Related Questions