Page 1 of 1

[AHK] Get paths of selected fileS or a file in XYplorer ?

Posted: 05 Dec 2015 01:21
by titep
In AHK there is a small script to get :
- paths of target window's selected items
- paths of all items in the target window's folder
- path of target window's folder

It works great, but I use XYplorer instead of Microsoft Default Explorer for quite long time because it lacks a number of functionalities. I have searched throughout the ahk forum but couldn't find any ahk script written for XYplorer. :?

How do you think about my suggestion? Is it possible and easy to do a similiar thing in XYplorer??

Thanks in advance.

Re: [AHK] Get paths of selected fileS or a file in XYplorer

Posted: 05 Dec 2015 01:53
by highend
Welcome to the club.

We have written quite a few scripts for XY in ahk / autoit here in this forum.

What exactly do you want to do with the aquired information from XY, in which way is the output formatted?

Link to the mentioned .ahk script for Windows Explorer?

Re: [AHK] Get paths of selected fileS or a file in XYplorer

Posted: 05 Dec 2015 04:36
by titep
highend wrote:Welcome to the club.

We have written quite a few scripts for XY in ahk / autoit here in this forum.

What exactly do you want to do with the aquired information from XY, in which way is the output formatted?

Link to the mentioned .ahk script for Windows Explorer?

Dear highend,

Thank you for your prompt response.

Here you are:
-----------------------------------------------------------------------------------

Code: Select all

/*
	Library for getting info from a specific explorer window (if window handle not specified, the currently active
	window will be used).  Requires AHK_L or similar.  Works with the desktop.  Does not currently work with save
	dialogs and such.


	Explorer_GetSelected(hwnd="")   - paths of target window's selected items
	Explorer_GetAll(hwnd="")        - paths of all items in the target window's folder
	Explorer_GetPath(hwnd="")       - path of target window's folder

	example:
		F1::
			path := Explorer_GetPath()
			all := Explorer_GetAll()
			sel := Explorer_GetSelected()
			MsgBox % path
			MsgBox % all
			MsgBox % sel
		return

	Joshua A. Kinnison
	2011-04-27, 16:12
*/

Explorer_GetPath(hwnd="")
{
	if !(window := Explorer_GetWindow(hwnd))
		return ErrorLevel := "ERROR"
	if (window="desktop")
		return A_Desktop
	path := window.LocationURL
	path := RegExReplace(path, "ftp://.*@","ftp://")
	StringReplace, path, path, file:///
	StringReplace, path, path, /, \, All

	; thanks to polyethene
	Loop
		If RegExMatch(path, "i)(?<=%)[\da-f]{1,2}", hex)
			StringReplace, path, path, `%%hex%, % Chr("0x" . hex), All
		Else Break
	return path
}

Explorer_GetAll(hwnd="")
{
	return Explorer_Get(hwnd)
}

Explorer_GetSelected(hwnd="")
{
	return Explorer_Get(hwnd,true)
}

Explorer_GetWindow(hwnd="")
{
	; thanks to jethrow for some pointers here
    WinGet, process, processName, % "ahk_id" hwnd := hwnd? hwnd:WinExist("A")
    WinGetClass class, ahk_id %hwnd%

	if (process!="explorer.exe")
		return
	if (class ~= "(Cabinet|Explore)WClass")
	{
		for window in ComObjCreate("Shell.Application").Windows
            ; Some window objects do not have the hwnd property and throw an
            ; exception below.
            ; try
            ; if (window.hwnd!="")
            ; foo = window.Name
            ; IfInString, foo, Windows Explorer
            ; {
                ; MsgBox abcde
            if (window.hwnd==hwnd)
                return window
            ; }
	}
	else if (class ~= "Progman|WorkerW")
		return "desktop" ; desktop found
}

Explorer_Get(hwnd="",selection=false)
{
	if !(window := Explorer_GetWindow(hwnd))
		return ErrorLevel := "ERROR"
	if (window="desktop")
	{
		ControlGet, hwWindow, HWND,, SysListView321, ahk_class Progman
		if !hwWindow ; #D mode
			ControlGet, hwWindow, HWND,, SysListView321, A
		ControlGet, files, List, % ( selection ? "Selected":"") "Col1",,ahk_id %hwWindow%
		base := SubStr(A_Desktop,0,1)=="\" ? SubStr(A_Desktop,1,-1) : A_Desktop
		Loop, Parse, files, `n, `r
		{
			path := base "\" A_LoopField
			IfExist %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")
}

Re: [AHK] Get paths of selected fileS or a file in XYplorer

Posted: 05 Dec 2015 09:56
by highend
Hacked together...

Code: Select all

/*
    XYGetPath()     - paths of target window's selected items
    XYGetAll()      - paths of all items in the target window's folder
    XYGetSelected() - path of target window's folder

    example:
        F1::
            path := XYGetPath()
            all := XYGetAll()
            sel := XYGetSelected()
            MsgBox % path
            MsgBox % all
            MsgBox % sel
        return

*/

; Get our own HWND (we have no visible window)
DetectHiddenWindows, On
G_OwnHWND := WinExist("Ahk_PID " DllCall("GetCurrentProcessId"))
G_OwnHWND += 0

; Get messages back from XYplorer
OnMessage(0x4a, "Receive_WM_COPYDATA")

F1::
    path := XYGetPath()
    all  := XYGetAll()
    sel  := XYGetSelected()
    MsgBox % path
    MsgBox % all
    MsgBox % sel
return


XYGetPath()
{
    return XY_Get()
}


XYGetAll()
{
   return XY_Get(true)
}


XYGetSelected()
{
   return XY_Get(, true)
}


XY_Get(bAll:=false, bSelection:=false)
{
    global dataReceived, G_OwnHWND

    xyQueryScript =
    ( LTrim Join
        ::
        if (!%bAll% && !%bSelection%) {
            $return = "<curpath>";
        } elseif (%bAll%) {
            $return = listpane(, , , "<crlf>");
        } elseif (%bSelection%) {
            $return = get("SelectedItemsPathNames", "<crlf>");
        }
        copydata %G_OwnHWND%, "$return", 2;
    )

    Send_WM_COPYDATA(xyQueryScript)

    return dataReceived
}


GetXYHWND() {
    IfWinActive, ahk_class ThunderRT6FormDC
    {
        WinGet, xyHwnd, ID, ahk_class ThunderRT6FormDC
    } else {
        WinGet, xyHwnd, List, ahk_class ThunderRT6FormDC
        if (xyHwnd)
            xyHwnd := xyHwnd1
    }

    return xyHwnd
}


Send_WM_COPYDATA(message) {
   xyHwnd := GetXYHWND()

   if !(xyHwnd)
       return

   size := StrLen(message)
   if !(A_IsUnicode) {
      VarSetCapacity(data, size * 2, 0)
      StrPut(message, &data, size, "UTF-16")
   } else {
      data := message
   }
   VarSetCapacity(COPYDATA, A_PtrSize * 3, 0)
   NumPut(4194305, COPYDATA, 0, "Ptr")
   NumPut(size * 2, COPYDATA, A_PtrSize, "UInt")
   NumPut(&data, COPYDATA, A_PtrSize * 2, "Ptr")
   result := DllCall("User32.dll\SendMessageW", "Ptr", xyHWND, "UInt", 74, "Ptr", 0, "Ptr", &COPYDATA, "Ptr")
   return
}


Receive_WM_COPYDATA(wParam, lParam) {
   global dataReceived

   stringAddress := NumGet(lParam + 2 * A_PtrSize)
   copyOfData := StrGet(stringAddress)
   cbData := NumGet(lParam + A_PtrSize) / 2
   StringLeft, dataReceived, copyOfData, cbData

   return
}


Re: [AHK] Get paths of selected fileS or a file in XYplorer

Posted: 05 Dec 2015 12:25
by titep
Thanks,

ಠ_ಠ I will try to understand what you have done. You're brilliant! How did you do that?

Re: [AHK] Get paths of selected fileS or a file in XYplorer

Posted: 05 Dec 2015 16:28
by binocular222
I already wrote it here (see the last example of retrieve full path of <curiem>):
http://www.xyplorer.com/xyfc/viewtopic. ... 233#p82412

Re: [AHK] Get paths of selected fileS or a file in XYplorer

Posted: 05 Dec 2015 20:56
by armsys
Dear Highend,
Thank you for some XYS coding techniques.
Howeve, I have trouble to under the syntax of (LTrim Join...):
highend wrote:Hacked together...

Code: Select all

    xyQueryScript =
    ( LTrim Join
        ::
        if (!%bAll% && !%bSelection%) {
            $return = "<curpath>";
        } elseif (%bAll%) {
            $return = listpane(, , , "<crlf>");
        } elseif (%bSelection%) {
            $return = get("SelectedItemsPathNames", "<crlf>");
        }
        copydata %G_OwnHWND%, "$return", 2;
    )
What's LTrim Join?
Thank you in advance.

Re: [AHK] Get paths of selected fileS or a file in XYplorer

Posted: 05 Dec 2015 21:14
by highend
It removes all trailing spaces / tabs and joins the lines into one afterwards.

Re: [AHK] Get paths of selected fileS or a file in XYplorer

Posted: 05 Dec 2015 21:30
by armsys
highend wrote:It removes all trailing spaces / tabs and joins the lines into one afterwards.
Thanks for your fast help.
But when experimenting in the address bar (Alt+F8), the following command won't work:

Code: Select all

:: echo (ltrim join :: "abc " . "def  ");
:naughty:

Re: [AHK] Get paths of selected fileS or a file in XYplorer

Posted: 05 Dec 2015 21:33
by armsys
I google the "Ltrim Join" and find that Highend is the sole inventor of the command.
In the manual I couldn't find it.

Re: [AHK] Get paths of selected fileS or a file in XYplorer

Posted: 05 Dec 2015 22:42
by highend
Em...

That is part of the AHK CODE, not of XY's (which begins with "::" and ends before the last closing ")")^^

Re: [AHK] Get paths of selected fileS or a file in XYplorer

Posted: 05 Dec 2015 22:52
by armsys
highend wrote:Em...
That is part of the AHK CODE, not of XY's (which begins with "::" and ends before the last closing ")")^^
Got it. Thank you.