Autohotkey 和 Windows 10:如何获取当前资源管理器路径

问题描述 投票:0回答:4

我使用autohotkey版本:1.0.48.05(因为我坚持使用activeaid)。 读取当前路径的脚本如下(一直有效到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")

我怎么怀疑在切换到Win10的同时似乎也切换了语言。 如果我在清理之前 MsgBox 输出了 %FullPath% WinGetText、完整路径、A 消息框%FullPath% 我在其他字符串中看到(显然由 CR 分隔): 地址:V:\Vertrieb\Prospects\MyFile

那么我需要如何调整正则表达式来提取该字符串!

最诚挚的问候 汉内斯

autohotkey regexp-replace
4个回答
12
投票
#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
        }
    }
}

5
投票

尝试:

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
投票

我花了很多时间才找到(对我来说)最好的解决方案。 也许它也适合你。

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

0
投票

感谢Relex的回答,在我做出改变后它对我有用。

现在AutoHotKey已经发布了v2.0.0,只需将

ComObjCreate
更改为
ComObject
即可使用:

; https://stackoverflow.com/a/39266503/18372121
GetActiveExplorerPath() {
    explorerHwnd := WinActive("ahk_class CabinetWClass")
    if (explorerHwnd)
    {
        for window in ComObject("Shell.Application").Windows ; Change to ComObject
        {
            if (window.hwnd==explorerHwnd)
                return window.Document.Folder.Self.Path
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.