Internet Explorer: activate tab Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Internet Explorer: activate tab

09 Jul 2017, 00:44

Methods to activate a tab in Internet Explorer include:
- use the Acc library to invoke the tab button on the DirectUIHWND control using accDoDefaultAction (won't work if there are too many tabs)
- press Ctrl+Tab repeatedly and check the Edit control text (not ideal)

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

So I'd be interested in any possible alternatives, such as interacting with a control or IE object.

Some code to open many urls, and some failed attempts at nudging an IE tab:

Code: Select all

q:: ;internet explorer - open 26 tabs
Loop, 26
{
	Run, % "https://en.wikipedia.org/wiki/" Chr(64+A_Index)
	;Run, % "iexplore.exe https://en.wikipedia.org/wiki/" Chr(64+A_Index) ;opened tabs in new windows
	Sleep 300
}
return

;q::
;note: affected IE's appearance just under the tab buttons
ControlGet, hCtl, Hwnd,, Frame Tab3, A
WinActivate, % "ahk_id " hCtl
return

;q::
vNum := 3
vKey := "{Home}"
vKey := "q"
ControlFocus, % "Frame Tab" vNum, A
ControlFocus, % "TabWindowClass" vNum, A
ControlFocus, % "Shell DocObject View" vNum, A
ControlFocus, % "Internet Explorer_Server" vNum, A
if 0
{
	ControlSend, % "Frame Tab" vNum, % vKey, A
	ControlSend, % "TabWindowClass" vNum, % vKey, A
	ControlSend, % "Shell DocObject View" vNum, % vKey, A
	ControlSend, % "Internet Explorer_Server" vNum, % vKey, A
}
return
==================================================

Note: the TabWindowClass controls contain the tab name. If you find TabWindowClassNN, then its associated controls will have the same NN. Although you can also check parent/child controls to find associated controls.

Note: WBGet can be used to latch onto an Internet Explorer_Server control, and then get document.url and document.title.
Basic Webpage Controls with JavaScript / COM - Tutorial - Tutorials - AutoHotkey Community
https://autohotkey.com/board/topic/4705 ... -tutorial/
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
phaleth
Posts: 38
Joined: 13 Apr 2015, 03:49

Re: Internet Explorer: activate tab

09 Jul 2017, 03:08

Following is prolly not 100% reliable but still way better than using ControlSend

Code: Select all

SetTitleMatchMode, 2

;MsgBox, 0, Current, % IEGet().LocationUrl

targetURL := "https://autohotkey.com/"
wb := IEGet()
For wb in ComObjCreate( "Shell.Application" ).Windows {
	If InStr( wb.LocationURL, targetURL ) && InStr( wb.FullName, "iexplore.exe" ) {
		;WinGet, targetWinId, ID,% "ahk_id" wb.hwnd
		WinGetTitle, winTitle, Internet Explorer ahk_class IEFrame ahk_exe iexplore.exe
		Loop {
			WinActivate, Internet Explorer ahk_class IEFrame ahk_exe iexplore.exe
			SendInput, {LCtrl Down}{Tab}{LCtrl Up}
			WinGetTitle, winTitle, Internet Explorer ahk_class IEFrame ahk_exe iexplore.exe
			;MsgBox, % winTitle
		} Until InStr(winTitle, wb.LocationName)
		MsgBox, 0, % A_Index, % wb.LocationName
		break
	}
}

IEGet()
{
	WinGetTitle, Name, ahk_class IEFrame
	Name := StrReplace(Name, " - Internet Explorer", "")
	For wb in ComObjCreate( "Shell.Application" ).Windows
		If ( wb.LocationName = Name ) && InStr( wb.FullName, "iexplore.exe" )
			Return wb
} ;written by Jethrow
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Internet Explorer: activate tab  Topic is solved

19 Aug 2017, 20:59

Tested on Windows 7 64-bit, Internet Explorer 11.

Code: Select all

q:: ;internet explorer - activate tab
DetectHiddenWindows, On
WinGet, vWinList, List, ahk_class TabThumbnailWindow
vPrompt := "", vIndex := 0, oArray := []
Loop, % vWinList
{
	hWnd := vWinList%A_Index%
	WinGetTitle, vWinTitle, % "ahk_id " hWnd
	if (vWinTitle = "Blank Page - Internet Explorer")
		continue
	vIndex += 1, oArray[vIndex] := hWnd
	vPrompt .= vIndex " " vWinTitle "`r`n"
}
vPosW := 600, vPosH := 600
InputBox, vInput,, % vPrompt,, % vPosW, % vPosH
if !ErrorLevel
{
	;WinActivate, % "ahk_id " oArray[vInput]
	ControlSend, ahk_parent, {Alt Down}{Alt Up}{Alt Down}{Alt Up}, % "ahk_id " oArray[vInput]
	DllCall("user32\SetForegroundWindow", Ptr,oArray[vInput])
}
oArray := ""
return
==================================================

I replaced WinActivate with ControlSend and SetForegroundWindow, because using WinActivate works, but it can flash the title bar a bit.

For the WinActivate command source code see: SetForegroundWindowEx, a custom AHK function in window.cpp (which is not a standard Winapi function, although SetForegroundWindow is).

Various bits of code relating to activating windows:

Code: Select all

WinActivate, % "ahk_id " hWnd
ControlSend, ahk_parent, {Alt Down}{Alt Up}{Alt Down}{Alt Up}, % "ahk_id " hWnd
DllCall("user32\SetForegroundWindow", Ptr,hWnd)
PostMessage, 0x1C,,,, % "ahk_id " hWnd ;WM_ACTIVATEAPP := 0x1C
DllCall("user32\ShowWindow", Ptr,hWnd, Int,5) ;SW_SHOW := 5
DllCall("user32\ShowWindow", Ptr,hWnd, Int,9) ;SW_RESTORE := 9
DllCall("user32\BringWindowToTop", Ptr,hWnd)
==================================================

As a bonus, here are some handy Internet Explorer window messages:
Internet Explorer Winset command list - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 07#p134907
How to open the context menu of the window's title bar - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 70#p165570
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
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Internet Explorer: activate tab

02 Oct 2017, 23:07

I have here 8 functions, intended to work similarly to the functions for Firefox and Chrome:
Firefox/Chrome, get tab names/focus tab - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=26947

Unfortunately although a lot of things are doable in Internet Explorer, I have had a lot of problems trying to activate a tab in Internet Explorer. It's possible to loop through the Internet Explorer_Server controls in Internet Explorer, and retrieve the titles/urls, however, even though you might have the control hWnd, I'm not sure of a method to then focus the tab.

Methods to activate a tab work as follows: activate a TabThumbnailWindow window (if you know the title), or, use Acc to invoke the activation of one of the tabs you can see listed on the screen, or, navigate using ctrl+tab to a tab you can't see (when there are too many tabs for Internet Explorer to show all at the same time (if you know the title or url).

Code: Select all

;based on Acc applied to the visible tabs (if there are too many tabs open, Internet Explorer only shows some tabs in the header row)
;JEE_IntExpGetVisTabCount(hWnd)
;JEE_IntExpGetVisTabNames(hWnd, vSep:="`n", vMode:="t", vSep2:="`t")
;JEE_IntExpFocusVisTabByNum(hWnd, vNum)
;JEE_IntExpFocusVisTabByName(hWnd, vTitle, vNum:=1, vUseDesc:=0)

;based on COM or sending ctrl+num/(shift+)ctrl+tab or activating a TabThumbnailWindow window
;JEE_IntExpGetTabCount(hWnd:=-1)
;JEE_IntExpGetTabNames(hWnd, vSep:="`n", vMode:="t", vSep2:="`t")
;JEE_IntExpFocusTabByNum(hWnd, vNum)
;JEE_IntExpFocusTabByName(hWnd, vTitle, vNum:=1)

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

JEE_IntExpGetVisTabCount(hWnd)
{
	ControlGet, hCtl, Hwnd,, TabBandClass1, % "ahk_id " hWnd
	oAcc := Acc_Get("Object", "4.1.4.1", 0, "ahk_id " hCtl)
	for _, oChild in Acc_Children(oAcc)
	{
		vTabText := oChild.accName(0)
		if (vTabText == "Scroll tab list backward")
		|| (vTabText == "Scroll tab list forward")
		|| (vTabText == "New tab (Ctrl+T)")
			continue
		vCount++
	}
	oAcc := oChild := ""
	return vCount
}

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

JEE_IntExpGetVisTabNames(hWnd, vSep:="`n", vMode:="t", vSep2:="`t")
{
	ControlGet, hCtl, Hwnd,, TabBandClass1, % "ahk_id " hWnd
	oAcc := Acc_Get("Object", "4.1.4.1", 0, "ahk_id " hCtl)
	vOutput := ""
	for _, oChild in Acc_Children(oAcc)
	{
		vTabText := oChild.accName(0)
		if (vTabText == "Scroll tab list backward")
		|| (vTabText == "Scroll tab list forward")
		|| (vTabText == "New tab (Ctrl+T)")
			continue
		oTemp := StrSplit(oChild.accDescription(0), "`r`n")
		if (vMode = "t")
			vOutput .= oTemp.1 vSep
		else if (vMode = "tu")
			vOutput .= oTemp.1 vSep2 oTemp.2 vSep
		else if (vMode = "u")
			vOutput .= oTemp.2 vSep
		else if (vMode = "ut")
			vOutput .= oTemp.2 vSep2 oTemp.1 vSep
	}
	vOutput := SubStr(vOutput, 1, -StrLen(vSep))
	oAcc := oChild := oTemp := ""
	return vOutput
}

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

JEE_IntExpFocusVisTabByNum(hWnd, vNum)
{
	ControlGet, hCtl, Hwnd,, TabBandClass1, % "ahk_id " hWnd
	oAcc := Acc_Get("Object", "4.1.4.1", 0, "ahk_id " hCtl)
	vCount := 0, vRet := 0
	for _, oChild in Acc_Children(oAcc)
	{
		vTabText := oChild.accName(0)
		if (vTabText == "Scroll tab list backward")
		|| (vTabText == "Scroll tab list forward")
		|| (vTabText == "New tab (Ctrl+T)")
			continue
		vCount++
		if (vNum = vCount)
		{
			oChild.accDoDefaultAction(0), vRet := vNum
			break
		}
	}
	oAcc := oChild := ""
	return vRet
}

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

;WinGet, hWnd, ID, ahk_class IEFrame
;vTitle1 := "Google"
;vTitle2 := "Google" "`r`n" "https://www.google.co.uk/"
;JEE_IntExpFocusVisTabByName(hWnd, vTitle1)
;JEE_IntExpFocusVisTabByName(hWnd, vTitle2, 1, 1)
;return

JEE_IntExpFocusVisTabByName(hWnd, vTitle, vNum:=1, vUseDesc:=0)
{
	ControlGet, hCtl, Hwnd,, TabBandClass1, % "ahk_id " hWnd
	oAcc := Acc_Get("Object", "4.1.4.1", 0, "ahk_id " hCtl)
	vCount := 0, vRet := 0
	for _, oChild in Acc_Children(oAcc)
	{
		if !vUseDesc
			vTabText := oChild.accName(0)
		else
			vTabText := oChild.accDescription(0)
		if (vTabText = vTitle)
			vCount++
		if (vCount = vNum)
		{
			oChild.accDoDefaultAction(0), vRet := vCount
			break
		}
	}
	oAcc := oChild := ""
	return vRet
}

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

JEE_IntExpGetTabCount(hWnd:=-1)
{
	vCount := 0
	for oWin in ComObjCreate("Shell.Application").Windows
		if (hWnd = oWin.HWND)
		|| ((hWnd = -1) && (oWin.Name = "Internet Explorer"))
			vCount++
	return vCount
}

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

;vMode: e.g. htud,
;h=hWnd (hex), t=title, u=url, h=hWnd (dec)
JEE_IntExpGetTabNames(hWnd, vSep:="`n", vMode:="t", vSep2:="`t")
{
	vOutput := ""
	for oWin in ComObjCreate("Shell.Application").Windows
		if (hWnd = oWin.HWND)
		|| ((hWnd = -1) && (oWin.Name = "Internet Explorer"))
		{
			Loop, Parse, vMode
			{
				if (A_LoopField = "t")
					vOutput .= oWin.document.title
				else if (A_LoopField = "u")
					vOutput .= oWin.document.url
				else if (A_LoopField = "h")
					vOutput .= Format("0x{:x}", oWin.HWND)
				else if (A_LoopField = "d")
					vOutput .= oWin.HWND
				vOutput .= A_Index=StrLen(vMode)?vSep:vSep2
			}
		}
	return vOutput
}

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

;the function is currently limited by the use of SendInput
;since the use ControlSend or an equivalent has so far failed
JEE_IntExpFocusTabByNum(hWnd, vNum)
{
	if !WinActive("ahk_id " hWnd)
		WinActivate, % "ahk_id " hWnd
	if (vNum > 1) && (vNum < 8)
		SendInput, % "^" vNum
	else if (vNum = (vCount := JEE_IntExpGetTabCount(hWnd)))
		SendInput, ^9
	else if (vNum - 8 <= vCount - vNum)
	{
		SendInput, ^8
		Sleep 100
		Loop, % vNum - 8
		{
			SendInput, ^{Tab}
			Sleep 100
		}
	}
	else
	{
		SendInput, ^9
		Sleep 100
		Loop, % vCount - vNum
		{
			SendInput, ^+{Tab}
			Sleep 100
		}
	}
}

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

;hWnd parameter is currently redundant, but kept for potential future use
;and to match other functions
JEE_IntExpFocusTabByName(hWnd, vTitle, vNum:=1)
{
	DetectHiddenWindows, On
	WinGet, vWinList, List, ahk_class TabThumbnailWindow
	vCount := 0
	Loop, % vWinList
	{
		hWnd := vWinList%A_Index%
		WinGetTitle, vWinTitle, % "ahk_id " hWnd
		if !(vWinTitle = vTitle " - Internet Explorer")
			continue
		vCount++
		if !(vCount = vNum)
			continue
		ControlSend, ahk_parent, {Alt Down}{Alt Up}{Alt Down}{Alt Up}, % "ahk_id " hWnd
		DllCall("user32\SetForegroundWindow", Ptr,hWnd)
		break
	}
}

;==================================================
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 322 guests