2

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

Marc B
  • 356,200
  • 43
  • 426
  • 500
HHeckner
  • 4,722
  • 4
  • 23
  • 33

3 Answers3

9
#IfWinActive, ahk_class CabinetWClass ; explorer

    F1:: MsgBox, % GetActiveExplorerPath()

    ; or
    F2:: 
        ActiveExplorerPath := GetActiveExplorerPath()
        MsgBox, % ActiveExplorerPath
    return

#IfWinActive

    
GetActiveExplorerPath() {
; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=69925
    explorerHwnd := WinActive("ahk_class CabinetWClass")
    if (explorerHwnd)
    {
        for window in ComObjCreate("Shell.Application").Windows
        {
            if (window.hwnd==explorerHwnd)
                return window.Document.Folder.Self.Path
        }
    }
}
user3419297
  • 9,537
  • 2
  • 15
  • 24
  • You can download the portable version of AHK_L to run the code or compile the script to an .exe using the compiler from https://autohotkey.com/download/. – user3419297 Sep 02 '16 at 05:24
  • There is a COM library that was written by Sean that works with old version of AutoHotkey 1.0, you'll have to modify the code above to work with it as it is sytactically different than built COM support you get with the latest version of AutoHotkey. Also, there is no AHK_L, it's just AutoHotkey 1.1 as Lexicos has taken over as main developer of AutoHotkey. – errorseven Sep 03 '16 at 17:19
5

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
}
errorseven
  • 2,672
  • 2
  • 14
  • 20
  • Nicely done; perhaps enclosing the `f1` hotkey definition in `#If WinActive("ahk_class CabinetWClass") || WinActive("ahk_class ExlorerWClass")` makes sense to limit the hotkey to File Explorer windows. Out of curiosity: I only ever seem to see `CabinetWClass` as the window class; when is it `ExplorerWClass`, and are there other classes too? – mklement0 Jul 20 '19 at 13:05
  • 1
    @mklement0 it's for compatibility with older versions of Windows (before Vista). See the docs for [WinTitle->ahk_group](https://www.autohotkey.com/docs/misc/WinTitle.htm#ahk_group) for a brief example. – Shenk Jun 29 '20 at 16:57
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%
izzaki
  • 258
  • 2
  • 4
  • 1
    I suggest to use `Send {Ctrl down}l{Ctrl up}` instead of `ControlClick`. Sometimes it didn't work for me. – trogper Mar 18 '19 at 20:27