Page 2 of 2

Re: Get current explorer window path

Posted: 20 Dec 2023, 07:56
by Tomshi
ntepa wrote:
15 Feb 2023, 02:54
This function can get the path in Windows 11 tabbed explorer:

Code: Select all

GetActiveExplorerPath() {
    hwnd := WinActive("ahk_class CabinetWClass")
    activeTab := 0
    try ControlGet, activeTab, Hwnd,, % "ShellTabWindowClass1", % "ahk_id" hwnd
    for w in ComObjCreate("Shell.Application").Windows {
        if (w.hwnd != hwnd)
            continue
        if activeTab {
            static IID_IShellBrowser := "{000214E2-0000-0000-C000-000000000046}"
            shellBrowser := ComObjQuery(w, IID_IShellBrowser, IID_IShellBrowser)
            DllCall(NumGet(numGet(shellBrowser+0)+3*A_PtrSize), "Ptr", shellBrowser, "UInt*", thisTab)
            if (thisTab != activeTab)
                continue
            ObjRelease(shellBrowser)
        }
        return w.Document.Folder.Self.Path
    }
}
Any chance for a v2 version? :cry: the comobjquery and numget stuff is a little beyond my knowledge scope and I'm hitting a brick wall with it

Re: Get current explorer window path

Posted: 20 Dec 2023, 14:40
by ntepa
Tomshi wrote:
20 Dec 2023, 07:56
Any chance for a v2 version?
The v1 script was actually based on a v2 script: viewtopic.php?f=83&t=109907

Re: Get current explorer window path

Posted: 15 Mar 2024, 07:42
by JBensimon
Anybody know whether a similar approach is available to get the path of the directory currently displayed by standard File Open or Save dialogs? Despite the similar appearance, they're not shell window objects, but are they some other sort of object with readable properties?

Thanks.

JB

Re: Get current explorer window path

Posted: 15 Mar 2024, 10:21
by Skrell
ntepa wrote:
15 Feb 2023, 02:54
This function can get the path in Windows 11 tabbed explorer:

Code: Select all

GetActiveExplorerPath() {
    hwnd := WinActive("ahk_class CabinetWClass")
    activeTab := 0
    try ControlGet, activeTab, Hwnd,, % "ShellTabWindowClass1", % "ahk_id" hwnd
    for w in ComObjCreate("Shell.Application").Windows {
        if (w.hwnd != hwnd)
            continue
        if activeTab {
            static IID_IShellBrowser := "{000214E2-0000-0000-C000-000000000046}"
            shellBrowser := ComObjQuery(w, IID_IShellBrowser, IID_IShellBrowser)
            DllCall(NumGet(numGet(shellBrowser+0)+3*A_PtrSize), "Ptr", shellBrowser, "UInt*", thisTab)
            if (thisTab != activeTab)
                continue
            ObjRelease(shellBrowser)
        }
        return w.Document.Folder.Self.Path
    }
}
Should this also work in Win10?

Re: Get current explorer window path

Posted: 15 Mar 2024, 11:34
by JBensimon
Yes, it does work in Win10 for me.

JB

Re: Get current explorer window path

Posted: 15 Mar 2024, 11:54
by Skrell
JBensimon wrote:
15 Mar 2024, 11:34
Yes, it does work in Win10 for me.

JB
TY!

Re: Get current explorer window path

Posted: 19 Mar 2024, 12:09
by KnIfER
it will freeze if any explorer window is frozen!!!

will freeze accessing ComObjCreate("Shell.Application").Windows

will freeze using ahk v1.

Re: Get current explorer window path

Posted: 20 Mar 2024, 07:48
by KnIfER
another olution, get path from address bar ( need acc library)

Code: Select all


GetActiveExplorerPath()
{
	Try
	{
		WinGetPos,x,y,w,h
		acc := Acc_ObjectFromPoint(null, x+w-300, y+90)
		f := acc && StartsWith(t := acc.accName(0), "Address:")
		if(!f) {
			Loop, 35 {
				acc := Acc_ObjectFromPoint(null, x+w-35*A_Index, y+90)
				if(acc && StartsWith(t := acc.accName(0), "Address:")) {
					f := 1
					break
				}
			}
		}
		if(f)
			return SubStr(t, 9)
		; explorerHwnd := WinActive...
	}
}

Re: Get current explorer window path

Posted: 20 Mar 2024, 09:37
by V0RT3X
And yet another option...
viewtopic.php?f=76&t=51793#p228408

Code: Select all

#IfWinActive, ahk_class CabinetWClass
q:: ;Explorer window - copy focused item path (if item selected) else folder path
#IfWinActive, ahk_class ExploreWClass
q:: ;Explorer window - copy focused item path (if item selected) else folder path
WinGet, hWnd, ID, A
ControlGetFocus, vCtlClassNN, % "ahk_id " hWnd
if !(vCtlClassNN = "DirectUIHWND3")
{
    SendInput, ^c
    return
}

vPath := ""
for oWin in ComObjCreate("Shell.Application").Windows
    if (oWin.HWND = hWnd)
    {
        vPath := oWin.Document.FocusedItem.Path
        if (vPath = "")
            vPath := oWin.Document.Folder.Self.Path
        else
        {
            vIsSelected := 0
            for oItem in oWin.Document.SelectedItems
                if (vPath = oItem.Path)
                {
                    vIsSelected := 1
                    break
                }
            if !vIsSelected
                vPath := oWin.Document.Folder.Self.Path
        }
        break
    }
oWin := oItem := ""
Clipboard := vPath
MsgBox, % vPath
return
#IfWinActive