Page 1 of 1

Help to find the active tab number in Firefox

Posted: 14 Mar 2018, 17:02
by carno
I would like to get the tab number of the active tab I am reading in Firefox. By tab number I am referring to the order (sequence) number of the tab from the left. If it is the left-most (first) tab, then the number should be 1. if it is the second tab from the left the number should be 2, etc.

Re: Help to find the active tab number in Firefox

Posted: 14 Mar 2018, 17:48
by jeeswg
- I have some half-answers.
- If the tab title is unique then these functions will work (in conjunction with WinGetTitle):
Firefox/Chrome, get tab names/focus tab - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 14#p139114
- If the tab title is not unique, then you can use code from the functions above, to get the screen coordinates of each tab button (using the Acc_Location function) and apply PixelGetColor to a tab, and the colour will tell you if the tab is focused or not.
- When I used AccViewer, I couldn't see any information that differentiated between the focused tab and the other tabs.
- There may be other methods that I don't know about.

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

Re: Help to find the active tab number in Firefox

Posted: 14 Mar 2018, 17:59
by carno
Thanks! Are you specifically referring to this part of your code?:

Code: Select all

JEE_FirefoxFocusTabByName(hWnd, vTitle, vNum=1)
{
oAcc := Acc_Get("Object", "4", 0, "ahk_id " hWnd)
vRet := 0
for each, oChild in Acc_Children(oAcc)
	if (oChild.accName(0) == "Browser tabs")
	{
		oAcc := Acc_Children(oChild)[1], vRet := 1
		break
	}
if !vRet
{
	oAcc := oChild := ""
	return
}

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

Re: Help to find the active tab number in Firefox  Topic is solved

Posted: 14 Mar 2018, 18:03
by jeeswg
Eureka! I think I've got it. I used some information that AccViewer doesn't show, but that arguably it *should* show.

Code: Select all

q:: ;Mozilla Firefox - get index of focused tab
WinGet, hWnd, ID, A
oAcc := Acc_Get("Object", "4", 0, "ahk_id " hWnd)
vRet := 0
for _, oChild in Acc_Children(oAcc)
	if (oChild.accName(0) == "Browser tabs")
	{
		oAcc := Acc_Children(oChild)[1], vRet := 1
		break
	}
if !vRet
{
	oAcc := oChild := ""
	return
}

vRet := 0
for _, oChild in Acc_Children(oAcc)
{
	;STATE_SYSTEM_SELECTED := 0x2
	if (oChild.accState(0) & 0x2)
		vRet := A_Index
}
oAcc := oChild := ""
MsgBox, % vRet
return

Re: Help to find the active tab number in Firefox

Posted: 14 Mar 2018, 18:24
by jeeswg
- I was referring to any of the Firefox functions, they all do roughly the same thing.
- Anyhow, that code needed some tidying up, to add a bit more indentation, and to remove the word 'each', and use := instead of = for default values, I've just fixed it in the original link.

Re: Help to find the active tab number in Firefox

Posted: 14 Mar 2018, 19:32
by carno
Thanks VERY much!! It works beautifully and that's all I wanted. Just the tab order number and it works like magic.