Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Can AutoHotKey retrieve file path of the selected file ?


  • Please log in to reply
30 replies to this topic
Rapte_Of_Suzaku
  • Members
  • 901 posts
  • Last active: Jul 08 2011 02:12 PM
  • Joined: 29 Feb 2008
@Learning one
I was planning on making a separate library for the explorer stuff eventually, but I'm a bit busy with moving at the moment. For now, here's my proof-of-concept code that I made earlier on:
/*
other approaches:
	(Sean) http://www.autohotkey.com/forum/viewtopic.php?t=20701

*/
f2::
	hwnd := hwnd ? hwnd : WinExist("A")
	WinGetClass class, ahk_id %hwnd%
	
	if (class="CabinetWClass" or class="ExploreWClass" or class="Progman")
		for window in ComObjCreate("Shell.Application").Windows
			MsgBox % window.LocationURL

RETURN


F4::
	reload
return
F3::
	path := Explorer_GetPath()
	all := Explorer_GetItems()
	sel := Explorer_GetSelection()


	all_msg := "name for every item:`n"
	for item in all
		all_msg .= item.name "`n"

	sel_msg := "path for selected items:`n"
	for item in sel
		sel_msg .= item.path "`n"

	msg := "path=" path "`n`n" all_msg "`n`n" sel_msg

	MsgBox,, Active Explorer Window Info, %msg%
	;c := Container("COM",Explorer_GetSelection(),"Path")
	;dout_o(c,"container")
return




Explorer_GetWindow(hwnd="")
{
	hwnd := hwnd ? hwnd : WinExist("A")
	WinGetClass class, ahk_id %hwnd%
	
	if (class="CabinetWClass" or class="ExploreWClass" or class="Progman")
		for window in ComObjCreate("Shell.Application").Windows
			if (window.hwnd==hwnd)
				return window
}
Explorer_GetShellFolderView(hwnd="")
{
	return Explorer_GetWindow(hwnd).Document
}


Explorer_GetPath(hwnd="")
{
	return Explorer_GetWindow(hwnd).LocationURL
}
Explorer_GetItems(hwnd="")
{
	return Explorer_GetShellFolderView(hwnd).Folder.Items
}
Explorer_GetSelection(hwnd="")
{
	return Explorer_GetShellFolderView(hwnd).SelectedItems
}

@zoran
I can't believe I missed the problem! Try this:
#a::
selection := FC("explorer","","selection")
MsgBox % selection.toList("`n")
return

The issue is that FC() returns an object. MsgBox can't print objects directly (perhaps I should make a feature request?). The FileContainer object is basically a Container, numerically indexed starting at 1. so selection[1] would get the first path of the selection. And there are bunches of methods available (like toList()). Hopefully that works?

Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009
Thanks Rapte. Here it is:
F1::MsgBox % Explorer_GetSelection()

Explorer_GetSelection(hwnd="") {
	hwnd := hwnd ? hwnd : WinExist("A")
	WinGetClass class, ahk_id %hwnd%
	if (class="CabinetWClass" or class="ExploreWClass" or class="Progman")
		for window in ComObjCreate("Shell.Application").Windows
			if (window.hwnd==hwnd)
    sel := window.Document.SelectedItems
	for item in sel
	ToReturn .= item.path "`n"
	return Trim(ToReturn,"`n")
}


jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009
Nice example - though it wouldn't work for the desktop. Is any other window class "Progman" other than the desktop? If not, you could modify your function as such:
Explorer_GetSelection(hwnd=[color=#666666]""[/color]) {

    [color=#107095]WinGet[/color], [color=#107095]process[/color], processName, % [color=#666666]"ahk_id"[/color] hwnd := hwnd? hwnd:[color=#107095]WinExist[/color]([color=#666666]"A"[/color])

    [color=#107095]WinGetClass[/color] class, ahk_id %hwnd%

    [color=#107095]if[/color] ([color=#107095]process[/color] = [color=#666666]"explorer.exe"[/color])

        [color=#107095]if[/color] (class ~= [color=#666666]"Progman|WorkerW"[/color]) {

            [color=#107095]ControlGet[/color], files, List, Selected Col1, SysListView321, ahk_class %class%

            [color=#107095]Loop[/color], Parse, files, `n, `r

                ToReturn .= [color=brown]A_Desktop[/color] [color=#666666]"\"[/color] [color=brown]A_LoopField[/color] [color=#666666]"`n"[/color]

        } [color=#107095]else[/color] [color=#107095]if[/color] (class ~= [color=#666666]"(Cabinet|Explore)WClass"[/color]) {

            [color=#107095]for[/color] window in [color=#107095]ComObjCreate[/color]([color=#666666]"Shell.Application"[/color]).Windows

                [color=#107095]if[/color] (window.hwnd==hwnd)

                    sel := window.Document.SelectedItems

            [color=#107095]for[/color] item in sel

                ToReturn .= item.path [color=#666666]"`n"[/color]

        }

    [color=#107095]return[/color] [color=#107095]Trim[/color](ToReturn,[color=#666666]"`n"[/color])

}


Rapte_Of_Suzaku
  • Members
  • 901 posts
  • Last active: Jul 08 2011 02:12 PM
  • Joined: 29 Feb 2008
That's how I currently do it (if not the current FC, then my test version). But there are other problems to address. Like the formatting of the paths and getting a list after using #D to show the desktop (need to add "WorkerW" as an alternative to "Progman" for that last case, but WorkerW looks to be widely used.) And I've not tested this all with save dialogs and such yet.

But "progman" I've never seen anywhere other than the desktop.

jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009

(need to add "WorkerW" as an alternative to "Progman" for that last case, but WorkerW looks to be widely used.)

Couldn't you just make sure the process is "explorer.exe"? I modified the code above.

Learning one
  • Members
  • 1483 posts
  • Last active: Jan 02 2016 02:30 PM
  • Joined: 04 Apr 2009
Nice final version of the function jethrow :)

This is really useful & practical function IMO.
:idea: Perhaps it would be beneficial for our community if somebody would post it in Scripts & Functions forum or in COM Object Reference [AutoHotkey_L] so others can find it easier... What do you think?

zoran
  • Members
  • 5 posts
  • Last active: Nov 28 2010 01:32 PM
  • Joined: 26 Nov 2010
Gentlimens,

it seems that we have here solution ..just one more question from the very begining of this discussion.
I would need to pass as input parameters to experiment.bat file all paths returned with:
Explorer_GetSelection()

I tried:

F8::Run, C:\_Zoki\AutoHotKey\experiment.bat % Explorer_GetSelection()%

and some other things it just does not go :(... do you know how exactlly to pass list of paths from Explorer_GetSelection() to batch file ?

best regards,
Zoki

  • Guests
  • Last active:
  • Joined: --
SelectedFiles := Explorer_GetSelection()

StringReplace, SelectedFiles, SelectedFiles, `n, " ", all

SelectedFiles = "%SelectedFiles%"



F8::Run, C:\_Zoki\AutoHotKey\experiment.bat %SelectedFiles%


Rapte_Of_Suzaku
  • Members
  • 901 posts
  • Last active: Jul 08 2011 02:12 PM
  • Joined: 29 Feb 2008
Updated the code and posted Explorer Library in the Scripts and Functions forum so that it is easier for others to find.

Puzzled Greatly
  • Members
  • 612 posts
  • Last active: May 22 2017 11:15 PM
  • Joined: 15 Aug 2009
I've been using these functions for a while now but suddenly I'm getting errors with Explorer_GetSelection(hwnd=""). The error says:

Warning: Using value of unitialized variable

. After the error message I am getting the correct path to the file. Explorer_GetPath(hwnd="") still works without error. Is ther any way to fix this or is there an alternative to getting the selected file path without using the clipboard. Thanks.

Rapte_Of_Suzaku
  • Members
  • 901 posts
  • Last active: Jul 08 2011 02:12 PM
  • Joined: 29 Feb 2008

I've been using these functions for a while now but suddenly I'm getting errors with Explorer_GetSelection(hwnd=""). The error says:

Warning: Using value of unitialized variable

. After the error message I am getting the correct path to the file. Explorer_GetPath(hwnd="") still works without error. Is ther any way to fix this or is there an alternative to getting the selected file path without using the clipboard. Thanks.


Hmmm... Probably something in a new version of AHK, but I haven't updated since L61H31. You should try putting msgboxes at various points in the code to see exactly what part causes the warning (I'd recommend my debugging library, but I assume that's stopped working at some point).

odd thought, but do you use
path:=Explorer_GetSelection()
or do you use
path:=Explorer_GetSelection(hwnd="")
?

Puzzled Greatly
  • Members
  • 612 posts
  • Last active: May 22 2017 11:15 PM
  • Joined: 15 Aug 2009

odd thought, but do you use

path:=Explorer_GetSelection()
or do you use
path:=Explorer_GetSelection(hwnd="")
?

I use the first. I tried the second but it gave the same error and didn't produce any path. Also, it doesn't like path used as the variable as it is used inside the function. I use ItemPath for my variable name.

I think it is ToReturn that is causing the problem but I don't know how to fix it.

Puzzled Greatly
  • Members
  • 612 posts
  • Last active: May 22 2017 11:15 PM
  • Joined: 15 Aug 2009
Fixed it. Added:
Explorer_GetSelection(hwnd="") {
	ToReturn =
        etc....


General Grandissmio
  • Members
  • 5 posts
  • Last active: Oct 29 2015 09:19 AM
  • Joined: 14 Mar 2015

Hi,
the script of the Learning One seems to be not working anymore?
Does anyone has an idea what could be causing this?



Jackie Sztuk _Blackholyman
  • Spam Officer
  • 3757 posts
  • Last active: Apr 03 2016 08:47 PM
  • Joined: 28 Feb 2012
hi,

the script in this post http://www.autohotke...-file/?p=382642

and the one in this one http://www.autohotke...-file/?p=383065

Both work fine for me on windows 7 with the newest version of AutoHotkey U 32bit

Do you get any errors and please also give us more info about your setup
Helping%20you%20learn%20autohotkey.jpg?d

[AHK] Version. 1.1+ [CLOUD] DropBox ; Copy [WEBSITE] Blog ; About