如何使用 ahk2.x 获取当前文件或文件夹路径和文件名?

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

我是新手,网上搜的都是AutoHotkey V1.

但我想使用 autohotkey V2 .

在 Windows 10 或 11 上获取当前选择的文件或文件夹路径和文件名。

autohotkey
1个回答
0
投票

原问题的答案: 如何获取当前文件或文件夹路径和文件名?

#Requires AutoHotkey v1.1

#NoEnv
#SingleInstance Force

; Create a group of the windows that contain files and/or folders:
; ahk_group ExplorerDesktopGroup
GroupAdd, ExplorerDesktopGroup, ahk_class ExploreWClass
GroupAdd, ExplorerDesktopGroup, ahk_class CabinetWClass
GroupAdd, ExplorerDesktopGroup, ahk_class Progman
GroupAdd, ExplorerDesktopGroup, ahk_class WorkerW

    return


#IfWinActive ahk_group ExplorerDesktopGroup

    ; Press Win+P in explorer or desktop 
    ; to get the path of the selected items in a message box

    #p::MsgBox % Explorer_GetSelection()

#IfWinActive

Explorer_GetSelection() {
    ; https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255169
    WinGetClass, winClass, % "ahk_id" . hWnd := WinExist("A")
    if !(winClass ~= "^(Progman|WorkerW|(Cabinet|Explore)WClass)$")
        Return   
    shellWindows := ComObjCreate("Shell.Application").Windows
    if (winClass ~= "Progman|WorkerW")
        shellFolderView := shellWindows.Item( ComObject(VT_UI4 := 0x13, SWC_DESKTOP := 0x8) ).Document
    else {
        for window in shellWindows 
        if (hWnd = window.HWND) && (shellFolderView := window.Document)
            break
   }
    for item in shellFolderView.SelectedItems
        result .= (result = "" ? "" : "`n") . item.Path
    Return result
}

编辑:

我设法使用 AHK-v2-script-converter 将它转换为 v2:

#Requires AutoHotkey v2.0

#SingleInstance Force

; Create a group of the windows that contain files and/or folders:
; ahk_group ExplorerDesktopGroup
GroupAdd("ExplorerDesktopGroup", "ahk_class ExploreWClass")
GroupAdd("ExplorerDesktopGroup", "ahk_class CabinetWClass")
GroupAdd("ExplorerDesktopGroup", "ahk_class Progman")
GroupAdd("ExplorerDesktopGroup", "ahk_class WorkerW")

    return


#HotIf WinActive("ahk_group ExplorerDesktopGroup")

    ; Press Win+P in explorer or desktop 
    ; to get the path of the selected items in a message box

    #p:: MsgBox(Explorer_GetSelection())

#HotIf

Explorer_GetSelection() {
    ; https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255169
    result := ""
    winClass := WinGetClass("ahk_id" . hWnd := WinExist("A"))
    if !(winClass ~= "^(Progman|WorkerW|(Cabinet|Explore)WClass)$")
        Return 
    shellWindows := ComObject("Shell.Application").Windows
    if (winClass ~= "Progman|WorkerW")
        shellFolderView := shellWindows.Item( ComValue(VT_UI4 := 0x13, SWC_DESKTOP := 0x8) ).Document
    else {
        for window in shellWindows 
        if (hWnd = window.HWND) && (shellFolderView := window.Document)
            break
   }
    for item in shellFolderView.SelectedItems
        result .= (result = "" ? "" : "`n") . item.Path
    Return result
}
相关问题
热门问答
最新问题