【问题标题】:Autohotkey and Windows 10: How to get current explorer pathAutohotkey 和 Windows 10:如何获取当前资源管理器路径
【发布时间】:2017-01-08 06:14:35
【问题描述】:

我使用自动热键版本: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,全路径,一个 消息框 %FullPath% 我在其他字符串中看到(显然由 CR 分隔): 地址:V:\Vertrieb\Prospects\MyFile

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

最好的问候 汉内斯

【问题讨论】:

    标签: autohotkey regexp-replace


    【解决方案1】:
    #IWinActive, ahk_class CabinetWClass ; explorer
    
        F1:: MsgBox, % GetActiveExplorerPath()
    
        ; or
        F2:: 
            ActiveExplorerPath := GetActiveExplorerPath()
            MsgBox, % ActiveExplorerPath
        return
    
    #IWinActive
    
    ; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=69925
    
    GetActiveExplorerPath() {
        explorerHwnd := WinActive("ahk_class CabinetWClass")
        if (explorerHwnd)
        {
            for window in ComObjCreate("Shell.Application").Windows
            {
                if (window.hwnd==explorerHwnd)
                    return window.Document.Folder.Self.Path
            }
        }
    }
    

    【讨论】:

    • 您可以下载AHK_L的便携版来运行代码或使用autohotkey.com/download的编译器将脚本编译为.exe。
    • 有一个由 Sean 编写的 COM 库,适用于旧版本的 AutoHotkey 1.0,您必须修改上面的代码才能使用它,因为它在语法上与内置的 COM 支持不同获取最新版本的 AutoHotkey。此外,没有 AHK_L,它只是 AutoHotkey 1.1,因为 Lexicos 已经接管了 AutoHotkey 的主要开发者。
    【解决方案2】:

    试试:

    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
    }
    

    【讨论】:

    • 干得好;也许在#If WinActive("ahk_class CabinetWClass") || WinActive("ahk_class ExlorerWClass") 中包含f1 热键定义对于将热键限制为文件资源管理器窗口是有意义的。出于好奇:我似乎只将CabinetWClass 视为窗口类;什么时候ExplorerWClass,还有其他课吗?
    • @mklement0 这是为了与旧版本的 Windows(Vista 之前)兼容。有关简短示例,请参阅WinTitle->ahk_group 的文档。
    【解决方案3】:

    我花了很多时间(对我而言)找到最佳解决方案。 也许它也对你有用。

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

    【讨论】:

    • 我建议使用Send {Ctrl down}l{Ctrl up} 而不是ControlClick。有时它对我不起作用。
    猜你喜欢
    • 1970-01-01
    • 2016-09-26
    • 2017-10-30
    • 1970-01-01
    • 2015-02-19
    • 2011-05-23
    • 1970-01-01
    • 2021-05-14
    • 2017-07-07
    相关资源
    最近更新 更多