Firefox/Chrome, get tab names/focus tab

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Firefox/Chrome, get tab names/focus tab

16 Jan 2017, 04:01

[For updated scripts, click url]
Firefox/Chrome, get tab names/focus tab - Page 2 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=26947&p=294316#p294316

==================================================

Note: To get the url for the active tab:
Get the URL of the current (active) browser tab - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=3702

Note: I don't have any code for getting the urls from tabs in general.
However, see Firefox/Chrome extensions:
Firefox/Chrome: copy titles/urls to the clipboard - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=22&t=66246

Bonus scripts:
Mozilla Firefox: scroll by small amounts - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=68578
Mozilla Firefox: Save Image As... using s not v - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=39310
htm/html files: identify the web browser that saved it - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=65683

==================================================

[EDIT:] Old versions of the code follow:

Why has nobody updated the Firefox get tab names/set active tab functions(/scripts), or made equivalent functions for Chrome?
Surely it's quite easy for the experts here?
And Firefox and Chrome are very common so there would be an immediate and obvious demand?

Well, I think I've managed to do it ...

JEE_FirefoxGetTabNames(hWnd, vSep="`n")
JEE_FirefoxFocusTabByNum(hWnd, vNum)
JEE_FirefoxFocusTabByName(hWnd, vTitle, vNum=1)
JEE_ChromeGetTabNames(hWnd, vSep="`n")
JEE_ChromeFocusTabByNum(hWnd, vNum)
JEE_ChromeFocusTabByName(hWnd, vTitle, vNum=1)

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

Working example script:
autohotkey - Using AHK to activate specific tabs in chrome or firefox - Stack Overflow
http://stackoverflow.com/questions/41478085/using-ahk-to-activate-specific-tabs-in-chrome-or-firefox

Mozilla Firefox

Code: Select all

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

JEE_FirefoxGetTabNames(hWnd, vSep:="`n")
{
	oAcc := Acc_Get("Object", "4", 0, "ahk_id " hWnd)
	Loop, % oAcc.accChildCount
		if (oAcc.accName(A_Index) = "Browser tabs")
		{
			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")
		{
			oAcc := Acc_Child(oAcc, A_Index)
			break
		}
	oAcc := Acc_Child(oAcc, 1)

	vRet := 0
	for _, oChild in Acc_Children(oAcc)
		if (A_Index = vNum)
		{
			oChild.accDoDefaultAction(0), vRet := A_Index
			break
		}
	oAcc := oChild := ""
	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")
		{
			oAcc := Acc_Child(oAcc, A_Index)
			break
		}
	oAcc := Acc_Child(oAcc, 1)

	vCount := 0, vRet := 0
	for _, 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
}

;==================================================
Google Chrome

Code: Select all

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

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 _, 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 := oChild := ""
	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 _, oChild in Acc_Children(oAcc)
	{
		if (A_Index = vNum+1)
		{
			oChild.accDoDefaultAction(0), vRet := A_Index
			break
		}
	}
	oAcc := oChild := ""
	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 _, 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
}

;==================================================
Note: These sorts of functions can easily break in future, and so will need amending.

Do post any issues or concerns.
Thanks for reading.
Last edited by jeeswg on 13 Oct 2019, 02:17, edited 10 times in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
sleepyswallow
Posts: 8
Joined: 04 Jan 2017, 18:56

Re: Firefox/Chrome, get tab names/focus tab

16 Jan 2017, 17:39

thanks so much, trying this now!!
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Firefox/Chrome, get tab names/focus tab

17 Jan 2017, 19:54

Btw the Firefox functions look for the words 'Browser tabs', so it would be interesting to know whether this has to be changed for non-English users, and if so, I'll include a warning. Cheers.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Drugwash
Posts: 850
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

Re: Firefox/Chrome, get tab names/focus tab

19 Jan 2017, 09:02

Can you elaborate where "Browser tabs" text should be retrieved from? I've just tried this with Pale Moon 25.8.1 and no joy.
Also, that Acc Viewer I tried to use on the Pale Moon window keeps the CPU at 100% usage so something's not right there.
Tested on XP-SP3, AHK 1.1.24.04 Unicode.

Oh and the correct link to StackOverflow would be: link
Part of my AHK work can be found here.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Firefox/Chrome, get tab names/focus tab

19 Jan 2017, 11:16

AFAIK Pale Moon is not the same thing as Mozilla Firefox, so there is no reason to expect the function to work with Pale Moon. Btw what class does Pale Moon have? For me Firefox has the class 'MozillaWindowClass'.

I always present links as 'title CRLF url'. Because urls can get corrupted, and titles can help to find websites when urls are changed. Also, titles can be found in search results.

Potentially, you could use AccViewer to discover a way to do this is Pale Moon, using my functions as a guide, and using the object hierarchy in AccViewer, however there were one or two complications I came across when doing this for Firefox/Chrome versus many other programs.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Drugwash
Posts: 850
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

Re: Firefox/Chrome, get tab names/focus tab

19 Jan 2017, 11:59

Pale Moon has the very same class as Firefox. They share same codebase.
20170119183047a.png
20170119183047a.png (136.14 KiB) Viewed 29418 times
The link in first post is incomplete, that's what I meant. Or at least it looks like that in my browser:
2017-01-19_18.29.32.png
2017-01-19_18.29.32.png (4.24 KiB) Viewed 29418 times
Played around a little more with Acc Viewer, its usage is not very intuitive. Here's the output:
20170119185334-crop.png
20170119185334-crop.png (32.4 KiB) Viewed 29418 times


I put a few OutputDebug commands in

Code: Select all

JEE_FirefoxGetTabNames()
and it's getting a value of zero for oAcc.accChildCount when calling

Code: Select all

Acc_Get("Object", "4", 0, "ahk_id " hWnd)
. However I get a final result with a string called "System" (there is no such tab) when calling

Code: Select all

Acc_Get("Object", "1", 0, "ahk_id " hWnd)
- that is with a value of one instead of four as second parameter. But I have no idea what that is, all this object encapsulation and abstractisation is irritating because user has no control over the inner working.

Are there any more details you could share, please?
Part of my AHK work can be found here.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Firefox/Chrome, get tab names/focus tab

19 Jan 2017, 12:18

Thanks for the notification about the link, I've fixed it now.

When investigating Firefox/Chrome.
I used AccViewer to investigate the hierarchies from the tab button up.
I also copied AccViewer and made two replacements, to help investigate the browsers' hierarchies from the top hWnd down:

Code: Select all

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

;replace:
~Lbutton Up::
;with:
q::
GetAccInfo()
~Lbutton Up::

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

;replace:
		SB_SetText("Path: " GetAccPath(Acc).path, 2)
;with:
DetectHiddenWindows, Off
WinGet, hWnd, ID, ahk_class Chrome_WidgetWin_1
;WinGet, hWnd, ID, ahk_class MozillaWindowClass
DetectHiddenWindows, On
Acc := Acc_ObjectFromWindow(hWnd)
		SB_SetText("Path: " GetAccPath(Acc).path, 2)

;===============
Hope that helps.

[EDIT:]
Also, try things like this:

Code: Select all

WinGet, hWnd, ID, A
oAcc := Acc_Get("Object", "2.2.2.4.1", 0, "ahk_id " hWnd)
MsgBox, % oAcc.accName(0)
MsgBox, % Acc_Location(oAcc).x " " Acc_Location(oAcc).y " " Acc_Location(oAcc).w " " Acc_Location(oAcc).h
MsgBox, % oAcc.accChildCount

;===============
vOutput := ""
for _, oChild in Acc_Children(oAcc)
	vOutput .= oChild.accName(0) "`r`n"

vOutput .= "`r`n"
Loop, % oAcc.accChildCount
	vOutput .= oAcc.accName(A_Index) "`r`n"

MsgBox, % vOutput
;===============
Last edited by jeeswg on 03 Oct 2018, 12:44, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: Firefox/Chrome, get tab names/focus tab

19 Jan 2017, 12:38

Drugwash wrote:Pale Moon has the very same class as Firefox. They share same codebase.
But one of the differences, is that Pale Moon removed accessibility features, required by ACC:
Accessibility features. Most people don't have a need for specialized accessibility features for custom input or display devices. This cuts down on the input complexity, and increases speed, but will, obviously, not be suitable for people who need these features.
Also, that's a lot of pink! :shock:
User avatar
Drugwash
Posts: 850
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

Re: Firefox/Chrome, get tab names/focus tab

19 Jan 2017, 12:56

Thank you Nextron, that explains it.
Thank you for the help jeeswg, I'll let this issue rest.

As for the pink, I guess there's something wrong with my head: I can't stand color green. Besides, the computer is the only place where I can actually see la vie en rose.
Part of my AHK work can be found here.
Rangnarok

Re: Firefox/Chrome, get tab names/focus tab

24 Mar 2017, 11:11

Hi Jeeswg,

I am trying to test your function to get tab name and number to use for another purpose. I am testing with the working example you posted on overstack, but encountered this error: http://imgur.com/a/Hr6J2

Could you have a look and see where i got it wrong please?

Thank you
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Firefox/Chrome, get tab names/focus tab

24 Mar 2017, 16:29

I have fixed the Firefox functions and improved the Chrome functions.
I have also added JEE_FirefoxGetTabCount and JEE_ChromeGetTabCount.

JEE_FirefoxGetTabCount(hWnd)
JEE_FirefoxGetTabNames(hWnd, vSep:="`n")
JEE_FirefoxFocusTabByNum(hWnd, vNum)
JEE_FirefoxFocusTabByName(hWnd, vTitle, vNum:=1)
JEE_ChromeGetTabCount(hWnd)
JEE_ChromeGetTabNames(hWnd, vSep:="`n")
JEE_ChromeFocusTabByNum(hWnd, vNum)
JEE_ChromeFocusTabByName(hWnd, vTitle, vNum:=1)

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

==================================================

Updated Mozilla Firefox functions:

Code: Select all

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

JEE_FirefoxGetTabCount(hWnd)
{
	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
	}

	vCount := 0
	for _, oChild in Acc_Children(oAcc)
		if !(oChild.accName(0) == "")
			vCount++
	oAcc := oChild := ""
	return vCount
}

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

JEE_FirefoxGetTabNames(hWnd, vSep:="`n")
{
	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
	}

	vOutput := ""
	for _, oChild in Acc_Children(oAcc)
	{
		vTabText := oChild.accName(0)
		if !(vTabText == "")
		;&& !(vTabText == "New Tab")
		;&& !(vTabText == "Open a new tab")
			vOutput .= vTabText vSep
	}
	vOutput := SubStr(vOutput, 1, -StrLen(vSep)) ;trim right
	oAcc := oChild := ""
	return vOutput
}

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

JEE_FirefoxFocusTabByNum(hWnd, vNum)
{
	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 || !Acc_Children(oAcc)[vNum]
		vNum := ""
	else
		Acc_Children(oAcc)[vNum].accDoDefaultAction(0)
	oAcc := oChild := ""
	return vNum
}

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

JEE_FirefoxFocusTabByName(hWnd, vTitle, vNum:=1)
{
	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
	}

	vCount := 0, vRet := 0
	for _, 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
}

;==================================================
Updated Google Chrome functions:

Code: Select all

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

JEE_ChromeGetTabCount(hWnd)
{
	oAcc := Acc_Get("Object", "4.1.2.2.2", 0, "ahk_id " hWnd)
	for _, oChild in Acc_Children(oAcc)
	{
		vTabText := oChild.accName(0)
		if !(vTabText == "")
		&& !(vTabText == "New Tab")
			vCount++
	}
	oAcc := oChild := ""
	return vCount
}

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

JEE_ChromeGetTabNames(hWnd, vSep:="`n")
{
	oAcc := Acc_Get("Object", "4.1.2.2.2", 0, "ahk_id " hWnd)
	vOutput := ""
	for _, oChild in Acc_Children(oAcc)
	{
		vTabText := oChild.accName(0)
		if !(vTabText == "")
		&& !(vTabText == "New Tab")
			vOutput .= vTabText vSep
	}
	vOutput := SubStr(vOutput, 1, -StrLen(vSep)) ;trim right
	oAcc := oChild := ""
	return vOutput
}

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

JEE_ChromeFocusTabByNum(hWnd, vNum)
{
	oAcc := Acc_Get("Object", "4.1.2.2.2", 0, "ahk_id " hWnd)
	if !Acc_Children(oAcc)[vNum+1]
		vNum := ""
	else
		Acc_Children(oAcc)[vNum+1].accDoDefaultAction(0)
	oAcc := ""
	return vNum
}

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

JEE_ChromeFocusTabByName(hWnd, vTitle, vNum:=1)
{
	oAcc := Acc_Get("Object", "4.1.2.2.2", 0, "ahk_id " hWnd)
	vCount := 0, vRet := 0
	for _, 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
}

;==================================================
General test script:
A script I used to test whether the Firefox/Chrome functions were working or not:

Code: Select all

q:: ;test Firefox/Chrome functions
WinGet, hWnd, ID, A
WinGetClass, vWinClass, % "ahk_id " hWnd

if (vWinClass = "MozillaWindowClass")
	vBrowser := "Firefox"
else if (vWinClass = "Chrome_WidgetWin_1")
	vBrowser := "Chrome"
else
	return

vPathChrome := "chrome.exe"
vPathFirefox := "firefox.exe"
vCount := JEE_%vBrowser%GetTabCount(hWnd)
if !(vCount >= 3)
	Loop, 3
	{
		Run, % vPath%vBrowser% " https://en.wikipedia.org/wiki/" Chr(64+A_Index)
		Sleep, 800
	}

JEE_%vBrowser%FocusTabByNum(hWnd, 2)
Sleep, 1000
JEE_%vBrowser%FocusTabByName(hWnd, "C - Wikipedia")
Sleep, 1000
vCount := JEE_%vBrowser%GetTabCount(hWnd)
MsgBox, % vCount "`r`n" JEE_%vBrowser%GetTabNames(hWnd, "`r`n")
return
A test script, and details, regarding the Firefox problem and possible solutions:

Code: Select all

q:: ;tests on Firefox (explanation of the problem)
WinGet, hWnd, ID, A

;what the script tries to do:
;oAcc1 = Firefox window's 4th child
;oAcc2 = oAcc1's child with the name 'Browser tabs' (e.g. the 23rd child)
;oAcc3 = oAcc2's 1st child
;after this point, in code not shown here, but that is in the functions,
;we loop through the children of oAcc3,
;they contain the text for the browser tabs

;VERSION 1 - this stopped working with error 0x80070057:
;oAcc := Acc_Get("Object", "4", 0, "ahk_id " hWnd)
;MsgBox, % oAcc.accChildCount
;Loop, % oAcc.accChildCount
;	if (oAcc.accName(A_Index) = "Browser tabs")
;	{
;		oAcc := Acc_Child(oAcc, A_Index)
;		break
;	}
;oAcc := Acc_Child(oAcc, 1)
;MsgBox, % oAcc.accChildCount

;VERSION 2A (faster) - had to be replaced with this:
oAcc := Acc_Get("Object", "4", 0, "ahk_id " hWnd)
MsgBox, % oAcc.accChildCount
for _, oChild in Acc_Children(oAcc)
	if (oChild.accName(0) == "Browser tabs")
	{
		oAcc := Acc_Children(oChild)[1]
		break
	}
MsgBox, % oAcc.accChildCount

;VERSION 2B (slower) - or alternatively it could be replaced with this:
oAcc := Acc_Get("Object", "4", 0, "ahk_id " hWnd)
MsgBox, % oAcc.accChildCount
Loop, % oAcc.accChildCount
	if (Acc_Children(oAcc)[A_Index].accName(0) = "Browser tabs")
	{
		;oAcc := Acc_Children(oAcc)[A_Index]
		;oAcc := Acc_Children(oAcc)[1]
		;or as a one-liner:
		oAcc := Acc_Children(Acc_Children(oAcc)[A_Index])[1]
		break
	}
MsgBox, % oAcc.accChildCount
return
Last edited by jeeswg on 03 Oct 2018, 12:12, edited 2 times in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Desiet

Re: Firefox/Chrome, get tab names/focus tab

28 Nov 2017, 06:08

Cannot get data to show up

Using Chrome 61

Code: Select all

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

JEE_ChromeGetTabCount(hWnd)
{
oAcc := Acc_Get("Object", "4.1.2.2.2", 0, "ahk_id " hWnd)
for each, oChild in Acc_Children(oAcc)
{
	vTabText := oChild.accName(0)
	if !(vTabText == "")
	&& !(vTabText == "New Tab")
		vCount++
}
oAcc := oChild := ""
return vCount
}

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

JEE_ChromeGetTabNames(hWnd, vSep="`n")
{
oAcc := Acc_Get("Object", "4.1.2.2.2", 0, "ahk_id " hWnd)
vOutput := ""
for each, oChild in Acc_Children(oAcc)
{
	vTabText := oChild.accName(0)
	if !(vTabText == "")
	&& !(vTabText == "New Tab")
		vOutput .= vTabText vSep
}
vOutput := SubStr(vOutput, 1, -StrLen(vSep)) ;trim right
oAcc := oChild := ""
return vOutput
}

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

JEE_ChromeFocusTabByNum(hWnd, vNum)
{
oAcc := Acc_Get("Object", "4.1.2.2.2", 0, "ahk_id " hWnd)
if !Acc_Children(oAcc)[vNum+1]
	vNum := ""
else
	Acc_Children(oAcc)[vNum+1].accDoDefaultAction(0)
oAcc := ""
return vNum
}

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

JEE_ChromeFocusTabByName(hWnd, vTitle, vNum=1)
{
oAcc := Acc_Get("Object", "4.1.2.2.2", 0, "ahk_id " hWnd)
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
}

MsgBox % vCount "`r`n" JEE_%vBrowser%GetTabNames(hWnd, "`r`n")
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Firefox/Chrome, get tab names/focus tab

28 Nov 2017, 17:29

- I tested the 4 Chrome functions just now, on Chrome v62, Windows 7, and they are still working.
- Did any of the 4 functions work for you?
- You can use AccViewer to see if you can retrieve the text:
Acc library (MSAA) and AccViewer download links - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=26201
- And if you are able to retrieve the text, there is some advice here, which may make it possible for you to fix the functions to work on your PC, and then you could report back to say what you changed.
Acc: get text from all window/control elements - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=40615

[EDIT:] I tested the 4 Firefox functions, on Firefox v57, and they are still working also.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Desiet

Re: Firefox/Chrome, get tab names/focus tab

28 Nov 2017, 19:11

jeeswg wrote:- I tested the 4 Chrome functions just now, on Chrome v62, Windows 7, and they are still working.
- Did any of the 4 functions work for you?
- You can use AccViewer to see if you can retrieve the text:
Acc library (MSAA) and AccViewer download links - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=26201
- And if you are able to retrieve the text, there is some advice here, which may make it possible for you to fix the functions to work on your PC, and then you could report back to say what you changed.
Acc: get text from all window/control elements - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=40615

[EDIT:] I tested the 4 Firefox functions, on Firefox v57, and they are still working also.
it worked very well at the end, I went to the irc room and tidbit helped me call the function correctly

thanks!
Haai Henkie
Posts: 1
Joined: 06 Jun 2018, 17:24
Contact:

Re: Firefox/Chrome, get tab names/focus tab

09 Jun 2018, 15:20

Hello Jeesgw,

Thank you for the Firefox/Chrome tab functions.

I also wanted to activate a specific tab, but the example at StackOverflow did not suit my needs. In the hope it is of use to other people I would like to share my use case and script.

I have an AutoHotkey.ahk that defines several hotkeys and active words for starting up programs or Web pages. I wanted to be able to activate the Chrome tab with Evernote if present, else open a new tab with Evernote. This is what I added to AutoHotkey.ahk:

Code: Select all

#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%
SetTitleMatchMode, RegEx

; active word to activate Evernote Web
::vrnt::

; In Chrome I have chosen "On start-up: Continue where you left off", so in case no Chrome window exists, 
; I want to start up Chrome first, so that a Evernote Web tab from a previous can be activated and
; 2 tabs with Evernote Web are prevented
if !WinExist("ahk_class Chrome_WidgetWin_1")
{
	Run, %ProgramFiles%\Google\Chrome\Application\chrome.exe
	WinWaitActive, ahk_class Chrome_WidgetWin_1
}

: Loop through alle Chrome Windows...
WinGet, hWnd, List, ahk_class Chrome_WidgetWin_1
Loop, %hWnd%
{
; ... and activate if focus can be put on a tab wiht 'Evernote Web' in the name.
	if ChromeFocusTabByName(hWnd%A_Index%, "Evernote Web")
		WinActivate
}
; if still no window with "Evernote Web - Google Chrome" is active, open the URL for Evernote Web
if !WinActive("Evernote Web - Google Chrome")
	Run, https://www.evernote.com/Login.action
return

; Jeeswg's function modified: the given vTitle now has to be part of the Tab name
; since the Tab name changes over time, 'Evernote Web' is the only constant part.
ChromeFocusTabByName(hWnd, vTitle, vNum:=1)
{
	oAcc := Acc_Get("Object", "4.1.2.2.2", 0, "ahk_id " hWnd)
	vCount := 0, vRet := 0
	for _, oChild in Acc_Children(oAcc)
	{
		vTabText := oChild.accName(0)
		if InStr(vTabText, vTitle)
			vCount++
		if (vCount = vNum)
		{
			oChild.accDoDefaultAction(0), vRet := A_Index
			break
		}
	}
	oAcc := oChild := ""
	return vRet
}
Cheers,

Haai Henkie
Pikoloco

Re: Firefox/Chrome, get tab names/focus tab

02 Oct 2018, 13:10

Chrome version Version 69 no longer works when you try to get the number of open tabs
carno
Posts: 265
Joined: 20 Jun 2014, 16:48

Re: Firefox/Chrome, get tab names/focus tab

02 Oct 2018, 20:05

Does this work with Chrome Version 49.0.2623.112?
Johnny R
Posts: 348
Joined: 03 Oct 2013, 02:07

Re: Firefox/Chrome, get tab names/focus tab

03 Oct 2018, 02:32

Seems, the functions doesn't work for me under Firefox v54.0.1 because the condition if !vRet is true every time and NULL is returned (cf. line 14 in the function JEE_FirefoxGetTabCount()).
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Firefox/Chrome, get tab names/focus tab

03 Oct 2018, 12:28

- @Desiet and @Haai Henkie: Thanks for your comments.
- @Pikoloco: I've got a fix that works for me below.
I changed this in all the Chrome functions: "4.1.2.2.2" to "4.1.2.2.1".
And for JEE_ChromeFocusTabByNum, I changed: [vNum+1] to [vNum].
- @carno and @Johnny R: I always have the latest versions of Firefox and Chrome, and so can't test older versions. People are welcome to post versions of the functions that work in older browser versions.
- If the functions don't work for you. You can usually fix them by changing 'x.x.x.x.x' etc:
Acc_Get("Object", "x.x.x.x.x", 0, "ahk_id " hWnd)
Using this script should help you to identify what numbers to use:
Acc: get text from all window/control elements - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=40615

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

- This script is useful for testing the functions, see 'general test script', here:
Firefox/Chrome, get tab names/focus tab - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 14#p139114

- Firefox functions still working in v62.

Code: Select all

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

JEE_FirefoxGetTabCount(hWnd)
{
	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
	}

	vCount := 0
	for _, oChild in Acc_Children(oAcc)
		if !(oChild.accName(0) == "")
			vCount++
	oAcc := oChild := ""
	return vCount
}

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

JEE_FirefoxGetTabNames(hWnd, vSep:="`n")
{
	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
	}

	vOutput := ""
	for _, oChild in Acc_Children(oAcc)
	{
		vTabText := oChild.accName(0)
		if !(vTabText == "")
		;&& !(vTabText == "New Tab")
		;&& !(vTabText == "Open a new tab")
			vOutput .= vTabText vSep
	}
	vOutput := SubStr(vOutput, 1, -StrLen(vSep)) ;trim right
	oAcc := oChild := ""
	return vOutput
}

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

JEE_FirefoxFocusTabByNum(hWnd, vNum)
{
	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 || !Acc_Children(oAcc)[vNum]
		vNum := ""
	else
		Acc_Children(oAcc)[vNum].accDoDefaultAction(0)
	oAcc := oChild := ""
	return vNum
}

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

JEE_FirefoxFocusTabByName(hWnd, vTitle, vNum:=1)
{
	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
	}

	vCount := 0, vRet := 0
	for _, 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
}

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

JEE_FirefoxGetFocusedTabNum(hWnd)
{
	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
	{
		vRet := 0
		for _, oChild in Acc_Children(oAcc)
		{
			;STATE_SYSTEM_SELECTED := 0x2
			if (oChild.accState(0) & 0x2)
			{
				vRet := A_Index
				break
			}
		}
	}
	oAcc := oChild := ""
	return vRet
}

;==================================================
- Updated Chrome functions tested with v69.

Code: Select all

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

JEE_ChromeGetTabCount(hWnd)
{
	oAcc := Acc_Get("Object", "4.1.2.2.1", 0, "ahk_id " hWnd)
	for _, oChild in Acc_Children(oAcc)
	{
		vTabText := oChild.accName(0)
		if !(vTabText == "")
		&& !(vTabText == "New Tab")
			vCount++
	}
	oAcc := oChild := ""
	return vCount
}

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

JEE_ChromeGetTabNames(hWnd, vSep:="`n")
{
	oAcc := Acc_Get("Object", "4.1.2.2.1", 0, "ahk_id " hWnd)
	vOutput := ""
	for _, oChild in Acc_Children(oAcc)
	{
		vTabText := oChild.accName(0)
		if !(vTabText == "")
		&& !(vTabText == "New Tab")
			vOutput .= vTabText vSep
	}
	vOutput := SubStr(vOutput, 1, -StrLen(vSep)) ;trim right
	oAcc := oChild := ""
	return vOutput
}

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

JEE_ChromeFocusTabByNum(hWnd, vNum)
{
	oAcc := Acc_Get("Object", "4.1.2.2.1", 0, "ahk_id " hWnd)
	if !Acc_Children(oAcc)[vNum]
		vNum := ""
	else
		Acc_Children(oAcc)[vNum].accDoDefaultAction(0)
	oAcc := ""
	return vNum
}

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

JEE_ChromeFocusTabByName(hWnd, vTitle, vNum:=1)
{
	oAcc := Acc_Get("Object", "4.1.2.2.1", 0, "ahk_id " hWnd)
	vCount := 0, vRet := 0
	for _, 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
}

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

JEE_ChromeGetFocusedTabNum(hWnd)
{
	oAcc := Acc_Get("Object", "4.1.2.2.1", 0, "ahk_id " hWnd)
	vRet := 0
	for _, oChild in Acc_Children(oAcc)
	{
		;STATE_SYSTEM_SELECTED := 0x2
		if (oChild.accState(0) & 0x2)
		{
			vRet := A_Index
			break
		}
	}
	oAcc := oChild := ""
	return vRet
}

;==================================================
Last edited by jeeswg on 10 Oct 2018, 05:33, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Pikoloco

Re: Firefox/Chrome, get tab names/focus tab

03 Oct 2018, 12:58

[quote="jeeswg"][/quote]
if i use this in chrome 68 what will happen? i do not have chrome 68 to test

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: imustbeamoron and 138 guests