[v2] Explorer Get Selection & XYplorer Get Selection

Post your working scripts, libraries and tools.
WKen
Posts: 186
Joined: 21 Feb 2023, 00:01

[v2] Explorer Get Selection & XYplorer Get Selection

26 Feb 2023, 23:25

Explorer Get Selection:

Code: Select all

;===================================================================================================
;AUTHOR   : ntepa -- https://www.autohotkey.com/boards/viewtopic.php?p=529074#p529074
;Function : Explorer Get Selection
;Created  : 2023-07-09
;Modified : 2023-09-30
;Version  : v1.0
;===================================================================================================
#Requires AutoHotkey v2.0

#HotIf WinActive("ahk_exe explorer.exe")

F3::msgbox explorerGetSelection()

#HotIf

explorerGetSelection(hwnd := WinExist("A")) {
    if !RegExMatch(WinGetClass(hwnd), "^((?<Desktop>Progman|WorkerW)|CabinetWClass)$", &Match)
        return
    shellWindows := ComObject("Shell.Application").Windows
    ; get desktop or explorer window.
    if Match.Desktop ; 0x13 = VT_UI4, 0x8 = SWC_DESKTOP
        window := shellWindows.Item(ComValue(0x13, 0x8)).Document
    else {
        try if activeTab := ControlGetHwnd("ShellTabWindowClass1", hwnd)
            for w in shellWindows {
                if w.hwnd != hwnd
                    continue
                if IsSet(activeTab) {
                    IID_IShellBrowser := "{000214E2-0000-0000-C000-000000000046}"
                    shellBrowser := ComObjQuery(w, IID_IShellBrowser, IID_IShellBrowser)
                    ComCall(3, shellBrowser, "uint*", &thisTab:=0)
                    if thisTab != activeTab
                        continue
                }
                window := w.Document
                break
            }
		catch
		    for w in shellWindows
			    try if (w.hwnd = hwnd) {
				    window := w.Document
				    break
				}
    }

    Items := ""
    for i, in window.SelectedItems {
        Items .= (A_Index > 1 ? "`n" : "") i.Path ; append each selected item and add a new line.
    } else
        return window.Folder.Self.Path

    return Items
}
Or

Code: Select all

;===================================================================================================
;AUTHOR   : https://www.autohotkey.com/boards/viewtopic.php?p=509165#p509165
;Function : Explorer Get Selection
;Created  : 2023-02-10
;Modified : 2023-02-26
;Version  : v1.2
;===================================================================================================
#Requires AutoHotkey v2.0.2

; Get the selected items, excluding shortcut item on the desktop
F3::
{
 selNoDeskLnk := explorerGet(hwnd:="",true)
 if(selNoDeskLnk != "ERROR" && selNoDeskLnk)
    msgbox selNoDeskLnk
}



; Get the selected items
F4::
{
 explorerSel := explorerGetSel(hwnd:="",true)
 if(explorerSel != "ERROR" && explorerSel)
    msgbox explorerSel
}

explorerGetSel(hwnd := '', selection := True)
{
    hwWindow := ''
    ret := ""
    Switch window := explorerGetWindow(hwnd)
	{
     Case '':
	       Return 'ERROR'
     Case 'desktop':
         Try hwWindow := ControlGetHwnd('SysListView321', 'ahk_class Progman')
         hwWindow := hwWindow || ControlGetHwnd('SysListView321', 'A')
         Loop Parse ListViewGetContent((selection ? 'Selected' : '') ' Col1', hwWindow), '`n', '`r'
          ret .= A_Desktop '\' A_LoopField '`n'
     Default:
         For item in selection ? window.document.SelectedItems : window.document.Folder.Items
         ret .= item.path '`n'
    }
/*
	if(ret)
	{
	   ret1 := StrSplit(ret, "`n",)[1]
	   SplitPath ret1, &name, &dir, &ext, &name_no_ext, &drive
	   msgbox "The name of the first selected item is:`n" name
	}
*/
    Return Trim(ret, '`n')
}



; All items, excluding desktop shortcut items
F5::
{
 items := explorerGet(hwnd:="", false)
 if(items != "ERROR" && items)
    msgbox items
}

explorerGet(hwnd:="",selection:=false)
{
    hwWindow := ''
    ret := ""
    Switch window := explorerGetWindow(hwnd)
	{
     Case '':
	       Return 'ERROR'
     Case 'desktop':
         Try hwWindow := ControlGetHwnd('SysListView321', 'ahk_class Progman')
         hwWindow := hwWindow || ControlGetHwnd('SysListView321', 'A')
         Loop Parse ListViewGetContent((selection ? 'Selected' : '') ' Col1', hwWindow), '`n', '`r'
		 {
		  If FileExist(A_Desktop '\' A_LoopField) ; ignore special icons like Computer (at least for now)
          ret .= A_Desktop '\' A_LoopField '`n'
		 }
     Default:
         For item in selection ? window.document.SelectedItems : window.document.Folder.Items
         ret .= item.path '`n'
    }

    Return Trim(ret, '`n')
}



; Get path
F6::
{
 curPath := explorerGetPath()
 if(curPath != "ERROR")
    msgbox curPath
}

explorerGetPath(hwnd:="")
{
	if !(window := explorerGetWindow(hwnd))
		return ErrorLevel := "ERROR"
	if (window="desktop")
		return A_Desktop
	path := window.LocationURL
	if (path="")
	path := window.LocationName
	path := RegExReplace(path, "ftp://.*@","ftp://")
	path := StrReplace(path, "file:///")
	path := StrReplace(path, "/", "\") 
	path := StrReplace(path, "file://", "//")
	
	Loop
		if RegExMatch(path, "i)(?<=%)[\da-f]{1,2}", &hex)
           path := StrReplace(path, "`%" hex[0], Chr("0x" . hex[0] ))
		Else Break

	return path
}



; If it is on the desktop
explorerGetWindow(hwnd := '')
{
 class := WinGetClass(hwnd := hwnd || WinExist('A'))
 Switch
 {
  Case WinGetProcessName(hwnd) != 'explorer.exe': Return
  Case class ~= 'Progman|WorkerW': Return 'desktop'
  Case class ~= '(Cabinet|Explore)WClass':
   For window in ComObject('Shell.Application').Windows
    Try If window.hwnd = hwnd
     Return window
 }
}
Or

Code: Select all

;===================================================================================================
;Function : Explorer Get Selection
;Created  : 2023-02-10
;Modified : 2023-02-26
;Version  : v1.2
;===================================================================================================
#Requires AutoHotkey v2.0.2

; Get the selected items, excluding shortcut item on the desktop
F3::
{
 selNoDeskLnk := explorerGet(hwnd:="",true)
 if(selNoDeskLnk != "ERROR" && selNoDeskLnk)
    msgbox selNoDeskLnk
}



; Get the selected items
F4::
{
 explorerSel := explorerGetSel(hwnd:="",true)
 if(explorerSel != "ERROR" && explorerSel)
    msgbox explorerSel
}

explorerGetSel(hwnd:="",selection:=true)
{
	if !(window := explorerGetWindow(hwnd))
		return ErrorLevel := "ERROR"

    ret := ""
	if (window="desktop")
	{
		hwWindow := ControlGetHwnd("SysListView321", "ahk_class Progman")
		if !hwWindow ; #D mode
			hwWindow := ControlGetHwnd("SysListView321", "A")
		files := ListViewGetContent(( "selection" ? "Selected":"") " Col1", hwWindow)
		base := SubStr(A_Desktop,0,1)=="\" ? SubStr(A_Desktop,1,-1) : A_Desktop
		Loop Parse, files, "`n", "`r"
		{
			path := base "\" A_LoopField
			ret .= path "`n"
		}
	}
	else
	{
		if selection
			collection := window.document.SelectedItems
		else
			collection := window.document.Folder.Items
		for item in collection
			ret .= item.path "`n"
	}
/*
	if(ret)
	{
	   ret1 := StrSplit(ret, "`n",)[1]
	   SplitPath ret1, &name, &dir, &ext, &name_no_ext, &drive
	   msgbox "The name of the first selected item is:`n" name
	}
*/
	return Trim(ret,"`n")
}



; All items, excluding desktop shortcut items
F5::
{
 items := explorerGet(hwnd:="", false)
 if(items != "ERROR" && items)
    msgbox items
}

explorerGet(hwnd:="",selection:=false)
{
	if !(window := explorerGetWindow(hwnd))
		return ErrorLevel := "ERROR"

    ret := ""
	if (window="desktop")
	{
		hwWindow := ControlGetHwnd("SysListView321", "ahk_class Progman")
		if !hwWindow ; #D mode
			hwWindow := ControlGetHwnd("SysListView321", "A")
		files := ListViewGetContent((selection ? "Selected" : "") " Col1", hwWindow)
		base := SubStr(A_Desktop,0,1)=="\" ? SubStr(A_Desktop,1,-1) : A_Desktop
		Loop Parse, files, "`n", "`r"
		{
			path := base "\" A_LoopField
			If FileExist(path) ; ignore special icons like Computer (at least for now)
			ret .= path "`n"
		}
	}
	else
	{
		if selection
			collection := window.document.SelectedItems
		else
			collection := window.document.Folder.Items
		for item in collection
			ret .= item.path "`n"
	}
	return Trim(ret,"`n")
}



; Get path
F6::
{
 curPath := explorerGetPath()
 if(curPath != "ERROR")
    msgbox curPath
}

explorerGetPath(hwnd:="")
{
	if !(window := explorerGetWindow(hwnd))
		return ErrorLevel := "ERROR"
	if (window="desktop")
		return A_Desktop
	path := window.LocationURL
	if (path="")
	path := window.LocationName
	path := RegExReplace(path, "ftp://.*@","ftp://")
	path := StrReplace(path, "file:///")
	path := StrReplace(path, "/", "\") 
	path := StrReplace(path, "file://", "//")
	
	Loop
		if RegExMatch(path, "i)(?<=%)[\da-f]{1,2}", &hex)
           path := StrReplace(path, "`%" hex[0], Chr("0x" . hex[0] ))
		Else Break

	return path
}



; If it is on the desktop
explorerGetWindow(hwnd:="")
{
    processName := WinGetProcessName("ahk_id " hwnd := hwnd? hwnd:WinExist("A"))
    class := WinGetClass("ahk_id " hwnd)

	if (processName!="explorer.exe")
		return
	if (class ~= "(Cabinet|Explore)WClass")
	{
		for window in ComObject("Shell.Application").Windows
			try if (window.hwnd==hwnd)
				return window
	}
	else if (class ~= "Progman|WorkerW") 
		return "desktop" ; desktop found
}
XYplorer Get Selection: viewtopic.php?p=509863#p509863

Return to “Scripts and Functions (v2)”

Who is online

Users browsing this forum: sanmaodo, songdg and 25 guests