Re: Firefox/Chrome, get tab names/focus tab
Posted: 03 Oct 2018, 15:45
@Pikoloco: The new functions might work, otherwise the old functions might work, otherwise you could try to modify them.
Let's help each other out
https://www.autohotkey.com/boards/
Code: Select all
JEE_FirefoxGetUrl(hWnd, vOpt:="")
{
local
Loop, 10
{
vIndex := A_Index
vAccPath := "application.tool_bar3.combo_box1.editable_text"
;vAccPath := "4.25.3.2"
if InStr(vOpt, "p") ;(pop-up window)
vAccPath := "application.tool_bar1.combo_box1.editable_text"
oAcc := Acc_Get("Object", vAccPath, 0, "ahk_id " hWnd)
if !ErrorLevel
break
Sleep, 100
}
if (vIndex = 10)
return
vUrl := ""
try vUrl := oAcc.accValue(0)
oAcc := ""
if !(vUrl = "") && !InStr(vUrl, "://")
vUrl := "http://" vUrl
return vUrl
}
Code: Select all
;acc path: 4.1.2.2.2.5.2
;acc path: 4.1.2.1.2.5.2
JEE_ChromeGetUrl(hWnd, vOpt:="")
{
local
oAcc := Acc_Get("Object", "4.1.2.2.2.5.2", 0, "ahk_id " hWnd)
if !IsObject(oAcc)
|| !(oAcc.accName(0) = "Address and search bar")
oAcc := Acc_Get("Object", "4.1.2.1.2.5.2", 0, "ahk_id " hWnd)
vUrl := oAcc.accValue(0)
oAcc := ""
return vUrl
}
Code: Select all
; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=26947
JEE_ChromeGetTabCount(hWnd){
oAcc := Acc_Get("Object", "4.1.2.1.1.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
}
Code: Select all
;==================================================
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
}
Code: Select all
;==================================================
;Firefox functions suite (tested on Firefox v69):
;requires Acc.ahk:
;Acc library (MSAA) and AccViewer download links - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=26201
;JEE_FirefoxAccInit(vValue)
;JEE_FirefoxGetUrl(hWnd:="", vOpt:="")
;JEE_FirefoxGetTabCount(hWnd:="")
;JEE_FirefoxGetTabNames(hWnd:="", vSep:="`n")
;JEE_FirefoxFocusTabByNum(hWnd:="", vNum:="")
;JEE_FirefoxFocusTabByName(hWnd:="", vTitle:="", vNum:="")
;JEE_FirefoxGetFocusedTabNum(hWnd:="")
;JEE_FirefoxAddressBarIsFoc(hWnd:="")
;JEE_FirefoxCloseOtherTabs(hWnd:="", vOpt:="", vNum:="")
;warning: JEE_FirefoxCloseOtherTabs:
;there is no separate Close button Acc element to do accDoDefaultAction on,
;so at present, each tab is focused, and Ctrl+W is sent to it, which is unreliable
;note: you can only get the url for the *active* tab via Acc,
;to get the urls for other tabs, you could use a browser extension, see:
;Firefox/Chrome: copy titles/urls to the clipboard - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=22&t=66246
;==================================================
;note: this function is redundant
;note: an equivalent function is needed for Chrome
JEE_FirefoxAccInit(vValue)
{
local
}
;==================================================
JEE_FirefoxGetUrl(hWnd:="", vOpt:="")
{
local
if (hWnd = "")
hWnd := WinExist("A")
Loop 10
{
vIndex := A_Index
vAccPath := "application.tool_bar3.combo_box1.editable_text"
;vAccPath := "4.25.3.2"
if InStr(vOpt, "p") ;(pop-up window)
vAccPath := "application.tool_bar1.combo_box1.editable_text"
oAcc := Acc_Get("Object", vAccPath, 0, "ahk_id " hWnd)
if !ErrorLevel
break
;Sleep(100)
DllCall("kernel32\Sleep", "UInt",100)
}
if (vIndex = 10)
return
vUrl := ""
try vUrl := oAcc.accValue(0)
oAcc := ""
if InStr(vOpt, "x")
{
if !(vUrl = "") && !InStr(vUrl, "://")
vUrl := "http://" vUrl
}
return vUrl
}
;==================================================
JEE_FirefoxGetTabCount(hWnd:="")
{
local
if (hWnd = "")
hWnd := WinExist("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
}
vCount := 0
for _, oChild in Acc_Children(oAcc)
{
;ROLE_SYSTEM_PUSHBUTTON := 0x2B
if (oChild.accRole(0) = 0x2B)
continue
vCount++
}
oAcc := oChild := ""
return vCount
}
;==================================================
JEE_FirefoxGetTabNames(hWnd:="", vSep:="`n")
{
local
if (hWnd = "")
hWnd := WinExist("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
}
vHasSep := !(vSep = "")
if vHasSep
vOutput := ""
else
oOutput := []
for _, oChild in Acc_Children(oAcc)
{
;ROLE_SYSTEM_PUSHBUTTON := 0x2B
if (oChild.accRole(0) = 0x2B)
continue
try vTabText := oChild.accName(0)
catch
vTabText := ""
if vHasSep
vOutput .= vTabText vSep
else
oOutput.Push(vTabText)
}
oAcc := oChild := ""
return vHasSep ? SubStr(vOutput, 1, -StrLen(vSep)) : oOutput
}
;==================================================
JEE_FirefoxFocusTabByNum(hWnd:="", vNum:="")
{
local
if (hWnd = "")
hWnd := WinExist("A")
if !vNum
return
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:="")
{
local
if (hWnd = "")
hWnd := WinExist("A")
if (vNum = "")
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:="")
{
local
if (hWnd = "")
hWnd := WinExist("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
break
}
}
oAcc := oChild := ""
return vRet
}
;==================================================
JEE_FirefoxAddressBarIsFoc(hWnd:="")
{
local
if (hWnd = "")
hWnd := WinExist("A")
oAcc := Acc_Get("Object", "4", 0, "ahk_id " hWnd)
vRet := 0
for _, oChild in Acc_Children(oAcc)
{
if (oChild.accName(0) == "Navigation Toolbar")
oAcc := oChild, vRet := 1
}
if !vRet
{
oAcc := oChild := ""
return
}
oAcc := Acc_Children(oAcc).6
oAcc := Acc_Children(oAcc).2
;STATE_SYSTEM_FOCUSED := 0x4
vIsFoc := !!(oAcc.accState(0) & 0x4)
oAcc := oChild := ""
return vIsFoc
}
;==================================================
;vOpt: L (close tabs to the left)
;vOpt: R (close tabs to the right)
;vOpt: LR (close other tabs)
;vOpt: (blank) (close other tabs)
;vNum: specify a tab other than the focused tab
JEE_FirefoxCloseOtherTabs(hWnd:="", vOpt:="", vNum:="")
{
local
if (hWnd = "")
hWnd := WinExist("A")
;vWinClass := WinGetClass("ahk_id " hWnd)
WinGetClass, vWinClass, % "ahk_id " hWnd
if !(vWinClass = "MozillaWindowClass")
return
if (vNum = "")
vNum := JEE_FirefoxGetFocusedTabNum(hWnd)
if (vOpt = "")
vOpt := "LR"
vDoCloseLeft := !!InStr(vOpt, "L")
vDoCloseRight := !!InStr(vOpt, "R")
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
oChildren := Acc_Children(oAcc)
vIndex := oChildren.Length() + 1
Loop % vIndex - 1
{
vIndex--
oChild := oChildren[vIndex]
;ROLE_SYSTEM_PUSHBUTTON := 0x2B
try
{
if (oChild.accRole(0) = 0x2B)
continue
}
if (vIndex = vNum)
continue
if (vIndex > vNum) && !vDoCloseRight
continue
if (vIndex < vNum) && !vDoCloseLeft
continue
;note: cf. Chrome, Firefox doesn't have a separate Close button element
;oChild2 := Acc_Children(oChild).4
;if (oChild2.accName(0) = "Close")
; oChild2.accDoDefaultAction(0)
;oChild2 := ""
;instead we focus each tab and send Ctrl+W to it
JEE_FirefoxFocusTabByNum(hWnd, vIndex)
;Sleep(500)
DllCall("kernel32\Sleep", "UInt",500)
;ControlSend("{Ctrl down}w{Ctrl up}",, "ahk_id " hWnd)
ControlSend, ahk_parent, {Ctrl down}w{Ctrl up}, % "ahk_id " hWnd
}
oAcc := oChild := ""
return vRet
}
;==================================================
Code: Select all
;==================================================
;Chrome functions suite (tested on Chrome v77):
;requires Acc.ahk:
;Acc library (MSAA) and AccViewer download links - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=26201
;JEE_ChromeAccInit(vValue)
;JEE_ChromeGetUrl(hWnd:="", vOpt:="")
;JEE_ChromeGetTabCount(hWnd:="")
;JEE_ChromeGetTabNames(hWnd:="", vSep:="`n")
;JEE_ChromeFocusTabByNum(hWnd:="", vNum:="")
;JEE_ChromeFocusTabByName(hWnd:="", vTitle:="", vNum:="")
;JEE_ChromeGetFocusedTabNum(hWnd:="")
;JEE_ChromeAddressBarIsFoc(hWnd:="")
;JEE_ChromeCloseOtherTabs(hWnd:="", vOpt:="", vNum:="")
;note: you can only get the url for the *active* tab via Acc,
;to get the urls for other tabs, you could use a browser extension, see:
;Firefox/Chrome: copy titles/urls to the clipboard - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=22&t=66246
;==================================================
;note: these Acc paths often change:
;Acc paths determined via:
;[JEE_AccGetTextAll function]
;Acc: get text from all window/control elements - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=40615
JEE_ChromeAccInit(vValue)
{
if (vValue = "U1")
return "4.1.2.1.2.5.2" ;address bar
if (vValue = "U2")
return "4.1.2.2.2.5.2" ;address bar
if (vValue = "T")
return "4.1.2.1.1.1" ;tabs (append '.1' to get the first tab)
}
;==================================================
JEE_ChromeGetUrl(hWnd:="", vOpt:="")
{
local
static vAccPath1 := JEE_ChromeAccInit("U1")
static vAccPath2 := JEE_ChromeAccInit("U2")
if (hWnd = "")
hWnd := WinExist("A")
oAcc := Acc_Get("Object", vAccPath1, 0, "ahk_id " hWnd)
if !IsObject(oAcc)
|| !(oAcc.accName(0) = "Address and search bar")
oAcc := Acc_Get("Object", vAccPath2, 0, "ahk_id " hWnd)
vUrl := oAcc.accValue(0)
oAcc := ""
if InStr(vOpt, "x")
{
if !(vUrl = "") && !InStr(vUrl, "://")
vUrl := "http://" vUrl
}
return vUrl
}
;==================================================
JEE_ChromeGetTabCount(hWnd:="")
{
local
static vAccPath := JEE_ChromeAccInit("T")
if (hWnd = "")
hWnd := WinExist("A")
oAcc := Acc_Get("Object", vAccPath, 0, "ahk_id " hWnd)
vCount := 0
for _, oChild in Acc_Children(oAcc)
{
;ROLE_SYSTEM_PUSHBUTTON := 0x2B
if (oChild.accRole(0) = 0x2B)
continue
vCount++
}
oAcc := oChild := ""
return vCount
}
;==================================================
JEE_ChromeGetTabNames(hWnd:="", vSep:="`n")
{
local
static vAccPath := JEE_ChromeAccInit("T")
if (hWnd = "")
hWnd := WinExist("A")
oAcc := Acc_Get("Object", vAccPath, 0, "ahk_id " hWnd)
vHasSep := !(vSep = "")
if vHasSep
vOutput := ""
else
oOutput := []
for _, oChild in Acc_Children(oAcc)
{
;ROLE_SYSTEM_PUSHBUTTON := 0x2B
if (oChild.accRole(0) = 0x2B)
continue
try vTabText := oChild.accName(0)
catch
vTabText := ""
if vHasSep
vOutput .= vTabText vSep
else
oOutput.Push(vTabText)
}
oAcc := oChild := ""
return vHasSep ? SubStr(vOutput, 1, -StrLen(vSep)) : oOutput
}
;==================================================
JEE_ChromeFocusTabByNum(hWnd:="", vNum:="")
{
local
static vAccPath := JEE_ChromeAccInit("T")
if (hWnd = "")
hWnd := WinExist("A")
if !vNum
return
oAcc := Acc_Get("Object", vAccPath, 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:="")
{
local
static vAccPath := JEE_ChromeAccInit("T")
if (hWnd = "")
hWnd := WinExist("A")
if (vNum = "")
vNum := 1
oAcc := Acc_Get("Object", vAccPath, 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:="")
{
local
static vAccPath := JEE_ChromeAccInit("T")
if (hWnd = "")
hWnd := WinExist("A")
oAcc := Acc_Get("Object", vAccPath, 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
}
;==================================================
JEE_ChromeAddressBarIsFoc(hWnd:="")
{
local
static vAccPath1 := JEE_ChromeAccInit("U1")
static vAccPath2 := JEE_ChromeAccInit("U2")
if (hWnd = "")
hWnd := WinExist("A")
oAcc := Acc_Get("Object", vAccPath1, 0, "ahk_id " hWnd)
if !IsObject(oAcc)
|| !(oAcc.accName(0) = "Address and search bar")
oAcc := Acc_Get("Object", vAccPath2, 0, "ahk_id " hWnd)
;STATE_SYSTEM_FOCUSED := 0x4
vIsFoc := !!(oAcc.accState(0) & 0x4)
oAcc := ""
return vIsFoc
}
;==================================================
;vOpt: L (close tabs to the left)
;vOpt: R (close tabs to the right)
;vOpt: LR (close other tabs)
;vOpt: (blank) (close other tabs)
;vNum: specify a tab other than the focused tab
JEE_ChromeCloseOtherTabs(hWnd:="", vOpt:="", vNum:="")
{
local
static vAccPath := JEE_ChromeAccInit("T")
if (hWnd = "")
hWnd := WinExist("A")
if (vNum = "")
vNum := JEE_ChromeGetFocusedTabNum(hWnd)
if (vOpt = "")
vOpt := "LR"
vDoCloseLeft := !!InStr(vOpt, "L")
vDoCloseRight := !!InStr(vOpt, "R")
oAcc := Acc_Get("Object", vAccPath, 0, "ahk_id " hWnd)
vRet := 0
oChildren := Acc_Children(oAcc)
vIndex := oChildren.Length() + 1
Loop % vIndex - 1
{
vIndex--
oChild := oChildren[vIndex]
;ROLE_SYSTEM_PUSHBUTTON := 0x2B
if (oChild.accRole(0) = 0x2B)
continue
if (vIndex = vNum)
continue
if (vIndex > vNum) && !vDoCloseRight
continue
if (vIndex < vNum) && !vDoCloseLeft
continue
oChild2 := Acc_Children(oChild).4
if (oChild2.accName(0) = "Close")
oChild2.accDoDefaultAction(0)
oChild2 := ""
}
oAcc := oChild := ""
return vRet
}
;==================================================
Code: Select all
JEE_FirefoxFocusTabByName(FF, TabName)
Sleep 200
ControlSend, ahk_parent, {%Key%}, ahk_class MozillaWindowClass
JEE_FirefoxFocusTabByName(FF, CurrentTitle)
Code: Select all
!F3::
DetectHiddenWindows, Off ;I don't know whether it's required
SetTitleMatchMode, 2 ;I don't know whether it's required
If (WinID := WinExist("A")) ;- active window
{
WinGetTitle, Title, ahk_id %WinID% ;title of a window or active browser card
;Clipboard := title
if title contains Mozilla ;cut Mozilla in the name
{
xxc:=" - Mozilla Firefox"
stringreplace,title,title,%xxc%,,all
}
Clipboard:=title
}
return
Code: Select all
WinActivate ahk_class MozillaWindowClass
WinWaitActive ahk_class MozillaWindowClass,, 1
if ErrorLevel
Return
Send, ^l
Clipboard:= ""
While !Clipboard && A_Index <= 5 {
Sleep 50
Send ^c
}
Send, {Tab 5}^{home} ; optional attepmpt to focuse site aria
;MsgBox % Clipboard ; Display result
Code: Select all
#F3::
sendinput,{F6}
Sleep 25
sendinput,^c
return
Code: Select all
JEE_ChromeGetUrl(hWnd:="0x1F0726", vOpt:="LR")
I'm using a fork of iswitchw.ahk (link: https://www.autohotkey.com/boards/viewtopic.php?f=6&t=89871), and it uses these lines:
Code: Select all
JEE_ChromeAccInit(vValue)
{
if (vValue = "U1")
return "4.1.2.1.2.5.2" ;address bar
if (vValue = "U2")
return "4.1.2.2.2.5.2" ;address bar
if (vValue = "T")
return "4.1.2.1.1.1" ;tabs (append '.1' to get the first tab)
}