autohotkey-2

How to get current file or folder path and filename with ahk2.x?


I am a novice, all I can search on the Internet are for AutoHotkey V1.

but i want use autohotkey V2 .

Get current choice file or folder path and filename on Windows 10 or 11.


Solution

  • An answer to your original question: How to get current file or folder path and filename?

    #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
    }
    

    EDIT:

    I managed to convert it to v2 using the AHK-v2-script-converter:

    #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
    }