Autohotkey and Windows 10: How to get current expl

2019-06-10 05:57发布

问题:

I use autohotkey version: 1.0.48.05 (because I stick with activeaid). The script to read the current path is as follows (and worked until Win 7).

; Get full path from open Explorer window
WinGetText, FullPath, A

; Clean up result
StringReplace, FullPath, FullPath, `r, , all
FullPath := RegExReplace(FullPath, "^.*`nAddress: ([^`n]+)`n.*$", "$1")

How I suspect that while switching to Win10 it seems that I also switched the language. If I MsgBox out the %FullPath% before cleaning with WinGetText, FullPath, A MsgBox %FullPath% I see amongst other strings (obvoíously separated by CR): Adresse: V:\Vertrieb\Prospects\MyFile

so how do I need to adjust the regexp to extract that very string!

Best regards Hannes

回答1:

#If WinActive("ahk_class CabinetWClass") ; explorer

    F1::
    WinGetTitle, ActiveTitle, A
    If InStr(ActiveTitle, "\")  ; If the full path is displayed in the title bar (Folder Options)
        Fullpath := ActiveTitle
    else
    If InStr(ActiveTitle, ":") ; If the title displayed is something like "DriveName (C:)"
    {
        Fullpath := SubStr(ActiveTitle, -2)
        Fullpath := SubStr(Fullpath, 1, -1)
    }
    else    ; If the full path is NOT displayed in the title bar 
    ; https://autohotkey.com/boards/viewtopic.php?p=28751#p28751
    for window in ComObjCreate("Shell.Application").Windows
    {
        try Fullpath := window.Document.Folder.Self.Path
        SplitPath, Fullpath, title
        If (title = ActiveTitle)
            break
    }
    MsgBox, %Fullpath%
    return 

#If


回答2:

Try:

f1::MsgBox % Explorer_GetSelection()

Explorer_GetSelection(hwnd="") {
    WinGet, process, processName, % "ahk_id" hwnd := hwnd? hwnd:WinExist("A")
    WinGetClass class, ahk_id %hwnd%
    if  (process = "explorer.exe") 
        if (class ~= "(Cabinet|Explore)WClass") {
            for window in ComObjCreate("Shell.Application").Windows
                if  (window.hwnd==hwnd)
                    path := window.Document.FocusedItem.path

            SplitPath, path,,dir
        }
        return dir
}


回答3:

It takes me so much time to find the best solution (for me). Maybe it will work for you as well.

ControlClick, ToolbarWindow323, A
ControlGetText, path, Edit1, A
Msgbox, %path%