Get path of current tab in Windows 11 Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
ntepa
Posts: 439
Joined: 19 Oct 2022, 20:52

Get path of current tab in Windows 11

19 Oct 2022, 21:38

The file explorer in Windows 11 now has tabs.
However, this function only returns the selected items or folder of the first tab. Is it possible to make this work with other tabs?

Code: Select all

#p::MsgBox  Explorer_GetSelection()

Explorer_GetSelection() {
	winClass := WinGetClass(hwnd := WinExist("A"))
	if !(winClass ~= "(Cabinet|Explore)WClass")
		Return
	for window in ComObject("Shell.Application").Windows
		if (window.hwnd = hwnd) && (oShellFolderView := window.Document)
			break
	result := ""
	for item in oShellFolderView.SelectedItems
		result .= (result = "" ? "" : "`n") . item.path
	Return result || oShellFolderView.Folder.Self.Path
}
lexikos
Posts: 9690
Joined: 30 Sep 2013, 04:07
Contact:

Re: Get path of current tab in Windows 11

21 Oct 2022, 18:47

All tabs in a window return the HWND of the window to which they belong, so if you break on the first iteration that matches a HWND, of course you'll only get one tab. This is probably the same as how Internet Explorer represents tabs in the Shell Windows collection, so you might look for how others have handled that.

As for determining which tab is active, I don't see any way within the shell window object model, but there's likely a way with Active Accessibility or UI Accessibility. A simpler approach would be to check the window title, but that won't work if there are tabs with identical names.
ntepa
Posts: 439
Joined: 19 Oct 2022, 20:52

Re: Get path of current tab in Windows 11

22 Oct 2022, 08:29

All the tabs in the window had the same hwnd. I couldn't find a COM property to tell them apart and ended up using UIA.

UIA library from here: https://github.com/thqby/ahk2_lib/blob/master/UIAutomation/UIAutomation.ahk

Code: Select all

#p::Msgbox ExplorerGetSelected()

ExplorerGetSelected()
{
	winClass := WinGetClass(hwnd := WinExist("A"))
	if !(winClass ~= "(Cabinet|Explore)WClass")
		Return
	el := UIA.ElementFromHandle(hwnd)
	addressEl := el.FindFirst(UIA.PropertyCondition({0:{ControlType:50021}, 1:{Name:"Address:", flags:2}, operator:"and"}))
	address := RegExReplace(addressEl.CurrentName, "^Address: ")
	CurrentPane := el.FindFirst(UIA.PropertyCondition({ControlType:50033, ClassName:"ShellTabWindowClass"}))
	listItems := el.FindAllWithOptions(UIA.PropertyCondition({ControlType:50007, SelectionItemIsSelected:ComValue(0xB, True)}), 0, CurrentPane)
	result := ""
	loop listItems.length {
		selectedListItem := listItems.GetElement(A_Index-1).CurrentName
		result .= address "\" selectedListItem "`n"
	}
	return result || address
}
lexikos
Posts: 9690
Joined: 30 Sep 2013, 04:07
Contact:

Re: Get path of current tab in Windows 11

22 Oct 2022, 20:14

All the tabs in the window had the same hwnd.
Yes, that is why your original code was only getting the first tab, as I explained. HWND is short for "handle to window". Same window = same HWND.
ntepa
Posts: 439
Joined: 19 Oct 2022, 20:52

Re: Get path of current tab in Windows 11

22 Oct 2022, 21:11

I was thinking of the tabs as separate windows because of the name windows in ComObject("Shell.Application").Windows.
The for loop still returns each tab as a ShellWindows object, but their hwnd property is the same.
My original code was not an attempt to get the active tab. It was there to show what I was using to get the folder path before tabs were introduced to Windows 11. My function was broken by the tabs feature because I could no longer use the hwnd property to get the current folder.
ntepa
Posts: 439
Joined: 19 Oct 2022, 20:52

Re: Get path of current tab in Windows 11

22 Oct 2022, 22:15

Sorry I didn't read your first sentence. I was just repeating what you said.
User avatar
JnLlnd
Posts: 490
Joined: 29 Sep 2013, 21:29
Location: Montreal, Quebec, Canada
Contact:

Re: Get path of current tab in Windows 11

27 Oct 2022, 14:57

FYI, this issue is also discussed in this thread of the v1 forum.
: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
ntepa
Posts: 439
Joined: 19 Oct 2022, 20:52

Re: Get path of current tab in Windows 11

28 Oct 2022, 00:23

This is much better than my attempt with UIA. Thank you!

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: bobstoner289, Draken and 60 guests