Open a folder in a new tab of Windows Explorer (Win 11 22H2)?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
JnLlnd
Posts: 490
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Open a folder in a new tab of Windows Explorer (Win 11 22H2)?

03 Nov 2022, 16:00

With Windows 11 22H2, Windows Explorer build number 22621.675 (or more) now has tabs. Sending ^t to the active Explorer window opens a new tab and make it active. Using this method, the code below opens a folder in a new Explorer tab.

But is there a better technique using, for example, a method of the Explorer COM object (similar to pExplorer.Navigate or pExplorer.Navigate2)?

Code: Select all

#Requires AutoHotkey v1.1
#NoEnv
#SingleInstance force

return

^1::
    Explorer_Navigate_New_Tab("C:\Windows")
Return

Explorer_Navigate_New_Tab(FullPath, hwnd="") {

	if (hwnd) { ; make sure this Explorer window is active
		WinActivate, ahk_id %hwnd%
		WinWaitActive, ahk_id %hwnd%
	}
	else
		hwnd := WinExist("A") ; if omitted, use active window
    WinGet, ProcessName, ProcessName, % "ahk_id " hwnd
    if (ProcessName != "explorer.exe")  ; not Windows explorer
        return
	
	; Windows Explorer is the active window
	Send, ^t ; add a new tab
	; Note 1: This hotkey works in English and French, don't know for other localizations
	; Note 2: AFAIK, this hotkey does nothing in earlier versions of Windows Explorer but
	; it would be safer to check the version before sending it
	Sleep, 100 ; for safety
	
	; the new tab is the active tab
    Explorer_Navigate_Tab("C:\Windows", hwnd)
}

Explorer_Navigate_Tab(FullPath, hwnd="") {
; see https://www.autohotkey.com/boards/viewtopic.php?p=489575#p489575
    hwnd := (hwnd="") ? WinExist("A") : hwnd ; if omitted, use active window
    WinGet, ProcessName, ProcessName, % "ahk_id " hwnd
    if (ProcessName != "explorer.exe")  ; not Windows explorer
        return
    For pExp in ComObjCreate("Shell.Application").Windows
    {
        if (pExp.hwnd = hwnd) { ; matching window found
			activeTab := 0
			try ControlGet, activeTab, Hwnd,, % "ShellTabWindowClass1", % "ahk_id" hwnd
			if activeTab {
				static IID_IShellBrowser := "{000214E2-0000-0000-C000-000000000046}"
				shellBrowser := ComObjQuery(pExp, IID_IShellBrowser, IID_IShellBrowser)
				DllCall(NumGet(numGet(shellBrowser+0)+3*A_PtrSize), "Ptr", shellBrowser, "UInt*", thisTab)
				if (thisTab != activeTab) ; matching active tab
					continue
				ObjRelease(shellBrowser)
			}
			pExp.Navigate("file:///" FullPath)
			return
		}
    }
}
:thumbup: Author of freeware Quick Access Popup, the powerful Windows folders, apps and documents launcher!
:P Now working on Quick Clipboard Editor
:ugeek: The Automator's Courses on AutoHotkey
robertel
Posts: 2
Joined: 23 Nov 2016, 23:40

Re: Open a folder in a new tab of Windows Explorer (Win 11 22H2)?

21 Sep 2023, 12:44

Nice!
I think you probably intended line 31 to be

Code: Select all

Explorer_Navigate_Tab(FullPath, hwnd)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], peter_ahk and 356 guests