Get path of selected file/folder or of current folder Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
partof
Posts: 110
Joined: 16 Jan 2016, 08:38

Get path of selected file/folder or of current folder

Post by partof » 28 Dec 2018, 07:32

When I am on a file explorer windows, I would like to get the path of the selected files/folders or of the current folder with a single keystroke. Here is my script, it's working except that I get backslash when I selected a file/folder and I get forward slash for the current folder. How can I get either all backslash or all forward slash.

To get here ,I have mix two scripts into a single one:
● script to get path of selected files/folder (but doesn't get path of current folder): https://autohotkey.com/board/topic/6072 ... ntry383065
● script to start a cmd in current folder (which is able to get the path of current folder): https://autohotkey.com/boards/viewtopic.php?t=5796

Code: Select all

#p::MsgBox % Explorer_GetSelection()
Explorer_GetSelection(hwnd="") {
	If WinActive("ahk_class CabinetWClass") || WinActive("ahk_class ExploreWClass") || WinActive("ahk_class Progman") {
	WinHWND := WinActive()
	For win in ComObjCreate("Shell.Application").Windows
		If (win.HWND = WinHWND) {
			sel := win.Document.SelectedItems
			for item in sel
			ToReturn .= item.path "`n"
			ToReturnFinal := Trim(ToReturn,"`n")
			if (ToReturnFinal == ""){
				dir := SubStr(win.LocationURL, 9) ; remove "file:///"
				ToReturnFinal := RegExReplace(dir, "%20", " ")
			}
		}
	}
	return %ToReturnFinal%
}

teadrinker
Posts: 4311
Joined: 29 Mar 2015, 09:41
Contact:

Re: Get path of selected file/folder or of current folder  Topic is solved

Post by teadrinker » 28 Dec 2018, 11:07

Hi
Your code doesn't work with the desktop window. Should be:

Code: Select all

#p::MsgBox % Explorer_GetSelection()

Explorer_GetSelection() {
   WinGetClass, winClass, % "ahk_id" . hWnd := WinExist("A")
   if !(winClass ~= "^(Progman|WorkerW|(Cabinet|Explore)WClass)$")
      Return
   
   shellWindows := ComObjCreate("Shell.Application").Windows
   if (winClass ~= "Progman|WorkerW")  ; IShellWindows::Item:    https://goo.gl/ihW9Gm
                                       ; IShellFolderViewDual:   https://goo.gl/gnntq3
      shellFolderView := shellWindows.Item( ComObject(VT_UI4 := 0x13, SWC_DESKTOP := 0x8) ).Document
   else {
      for window in shellWindows       ; ShellFolderView object: https://tinyurl.com/yh92uvpa
         if (hWnd = window.HWND) && (shellFolderView := window.Document)
            break
   }
   for item in shellFolderView.SelectedItems
      result .= (result = "" ? "" : "`n") . item.Path
   ;~ if !result
      ;~ result := shellFolderView.Folder.Self.Path
   Return result
}
Last edited by teadrinker on 17 Nov 2022, 12:47, edited 2 times in total.

partof
Posts: 110
Joined: 16 Jan 2016, 08:38

Re: Get path of selected file/folder or of current folder

Post by partof » 28 Dec 2018, 14:37

Super, thanks so much! I didn't notice it didn't work on desktop! That super nice of you to point this out!
This little piece of code, will save me hours!

User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Get path of selected file/folder or of current folder

Post by jeeswg » 29 Dec 2018, 03:31

It may be possible to interact with the desktop somewhat more simply. Cheers.
Create new file in current explorer window? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 361#p53361
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

teadrinker
Posts: 4311
Joined: 29 Mar 2015, 09:41
Contact:

Re: Get path of selected file/folder or of current folder

Post by teadrinker » 29 Dec 2018, 05:29

jeeswg, thanks, interesting.
It turns, it's not necessary to pass the parameter for hwnd.

Code: Select all

#p::MsgBox % Explorer_GetSelection()

Explorer_GetSelection() {
   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.FindWindowSW(0, 0, SWC_DESKTOP := 8, 0, SWFO_NEEDDISPATCH := 1).Document
   else {
      for window in shellWindows
         if (hWnd = window.HWND) && (shellFolderView := window.Document)
            break
   }
   for item in shellFolderView.SelectedItems
      result .= (result = "" ? "" : "`n") . item.Path
   if !result
      result := shellFolderView.Folder.Self.Path
   Return result
}

teadrinker
Posts: 4311
Joined: 29 Mar 2015, 09:41
Contact:

Re: Get path of selected file/folder or of current folder

Post by teadrinker » 29 Dec 2018, 07:00

Another method:

Code: Select all

shellWindows := ComObjCreate("Shell.Application").Windows
window := shellWindows.Item(ComObject(VT_UI4 := 0x13, SWC_DESKTOP := 8))
shellFolderView := window.Document
for item in shellFolderView.Folder.Items
   items .= (!items ? "" : "`n") . item.Path
MsgBox, % items
But this and two above will not work on Windows XP since it hasn't SWC_DESKTOP.

partof
Posts: 110
Joined: 16 Jan 2016, 08:38

Re: Get path of selected file/folder or of current folder

Post by partof » 29 Dec 2018, 15:29

You are amazing!

User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: Get path of selected file/folder or of current folder

Post by SKAN » 03 May 2020, 03:42

teadrinker wrote:
29 Dec 2018, 07:00
Another method:
Spoiler
But this and two above will not work on Windows XP since it hasn't SWC_DESKTOP.
@teadrinker I need this. Thank you very much.
My Scripts and Functions: V1  V2


User avatar
submeg
Posts: 326
Joined: 14 Apr 2017, 20:39
Contact:

Re: Get path of selected file/folder or of current folder

Post by submeg » 30 Jul 2021, 04:36

teadrinker wrote:
29 Dec 2018, 05:29

Code: Select all

#p::MsgBox % Explorer_GetSelection()

Explorer_GetSelection() {
   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.FindWindowSW(0, 0, SWC_DESKTOP := 8, 0, SWFO_NEEDDISPATCH := 1).Document
   else {
      for window in shellWindows
         if (hWnd = window.HWND) && (shellFolderView := window.Document)
            break
   }
   for item in shellFolderView.SelectedItems
      result .= (result = "" ? "" : "`n") . item.Path
   if !result
      result := shellFolderView.Folder.Self.Path
   Return result
}
@teadrinker, I would just like to say thanks for this gem of a code! I just took the main guts of this code and used it to create a tool that adds a prefix to the selected files. Thanks for sharing this, much appreciated.
____________________________________
Check out my site, submeg.com
Connect with me on LinkedIn
Courses on AutoHotkey :ugeek:

teadrinker
Posts: 4311
Joined: 29 Mar 2015, 09:41
Contact:

Re: Get path of selected file/folder or of current folder

Post by teadrinker » 30 Jul 2021, 08:54

@submeg :wave:
Instead of

Code: Select all

shellFolderView := shellWindows.FindWindowSW(0, 0, SWC_DESKTOP := 8, 0, SWFO_NEEDDISPATCH := 1).Document
there is a shorter option:

Code: Select all

shellFolderView := shellWindows.Item( ComObject(VT_UI4 := 0x13, SWC_DESKTOP := 0x8) ).Document

User avatar
submeg
Posts: 326
Joined: 14 Apr 2017, 20:39
Contact:

Re: Get path of selected file/folder or of current folder

Post by submeg » 30 Jul 2021, 09:11

Seriously?! That's much better! Nice!
____________________________________
Check out my site, submeg.com
Connect with me on LinkedIn
Courses on AutoHotkey :ugeek:

LAPIII
Posts: 667
Joined: 01 Aug 2021, 06:01

Re: Get path of selected file/folder or of current folder

Post by LAPIII » 17 Mar 2022, 21:50

How can this copy the path clipboard:

Code: Select all

#p::MsgBox % Explorer_GetSelection()
Explorer_GetSelection(hwnd="") {
	If WinActive("ahk_class CabinetWClass") || WinActive("ahk_class ExploreWClass") || WinActive("ahk_class Progman") {
	WinHWND := WinActive()
	For win in ComObjCreate("Shell.Application").Windows
		If (win.HWND = WinHWND) {
			sel := win.Document.SelectedItems
			for item in sel
			ToReturn .= item.path "`n"
			ToReturnFinal := Trim(ToReturn,"`n")
			if (ToReturnFinal == ""){
				dir := SubStr(win.LocationURL, 9) ; remove "file:///"
				ToReturnFinal := RegExReplace(dir, "%20", " ")
			}
		}
	}
	return %ToReturnFinal%
}

scriptor2016
Posts: 844
Joined: 21 Dec 2015, 02:34

Re: Get path of selected file/folder or of current folder

Post by scriptor2016 » 17 Mar 2022, 23:19

Code: Select all

+z::

Clipboard =
Send, ^c
ClipWait, 1 
Clipboard = %Clipboard%
MsgBox, Path of selected file is %ClipBoard% 

newcod3r
Posts: 505
Joined: 30 Sep 2021, 02:16

Re: Get path of selected file/folder or of current folder

Post by newcod3r » 18 May 2022, 22:20

teadrinker wrote:
29 Dec 2018, 07:00
Another method:

Code: Select all

shellWindows := ComObjCreate("Shell.Application").Windows
window := shellWindows.Item(ComObject(VT_UI4 := 0x13, SWC_DESKTOP := 8))
shellFolderView := window.Document
for item in shellFolderView.Folder.Items
   items .= (!items ? "" : "`n") . item.Path
MsgBox, % items
But this and two above will not work on Windows XP since it hasn't SWC_DESKTOP.
Hi there,

I wonder how this can be tweaked so that my script can also work in Q-Dir? I have included the proper ahk class inside but it still doesn't work.

ATL:00000001401AC840

Code: Select all

^f2:: ;Insert date based on file created date
For itemNum, item in Explorer_GetSelection()
FileGetTime, ctime, %item%, C
Clipboard =
FormatTime, Clipboard, %ctime%, yy-MM-dd
ClipWait
If ErrorLevel
 MsgBox, 48, Error, An error occurred while waiting for the clipboard.
;Else MsgBox,, Copied to clipboard, % Clipboard "`n`n(For " RegExReplace(item, ".+\\") ")"
Else SendInput {f2}^v{space}
Return

Explorer_GetSelection() {
 ; https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255256
 WinGetClass, winClass, % "ahk_id" hWnd := WinExist("A")
 If !(winClass ~= "Progman|WorkerW|(Cabinet|Explore)WClass|ATL:00000001401AC840")
  Return
 shellWindows := ComObjCreate("Shell.Application").Windows, sel := []
 If !(winClass ~= "Progman|WorkerW|ATL:00000001401AC840") {
  For window in shellWindows
   If (hWnd = window.HWND) && (shellFolderView := window.Document)
    Break
 } Else shellFolderView := shellWindows.FindWindowSW(0, 0, SWC_DESKTOP := 8, 0, SWFO_NEEDDISPATCH := 1).Document
 For item in shellFolderView.SelectedItems
  sel.Push(item.Path)
 Return sel
}

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Get path of selected file/folder or of current folder

Post by BoBo » 18 May 2022, 23:18

Code: Select all

F12::
clipboard:=""
Send +{F10}a  ; Windows context menus native "copy path" option (the trailing character is language setting related)
ClipWait
MsgBox % clipboard
Return
HTH

newcod3r
Posts: 505
Joined: 30 Sep 2021, 02:16

Re: Get path of selected file/folder or of current folder

Post by newcod3r » 19 May 2022, 00:10

Hi @BoBo , think this is not in response to my question? Thank you.

william_ahk
Posts: 482
Joined: 03 Dec 2018, 20:02

Re: Get path of selected file/folder or of current folder

Post by william_ahk » 12 Mar 2023, 08:14

teadrinker, great script! It has run many times on my machine. :thumbup:
I added support for non-explorer file lists (such as Everything) that supports copying file paths with ^c.

Code: Select all

GetSelectedFilePaths() {
	WinGetClass, winClass, % "ahk_id" . hWnd := WinExist("A")
	if (winClass ~= "Progman|WorkerW|(Cabinet|Explore)WClass") {
		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
		if !result
			result := shellFolderView.Folder.Self.Path
	} else {
		ClipSave := ClipboardAll
		Clipboard := ""
		SendInput ^c
		ClipWait 2
		result := Clipboard
		Clipboard := ClipSave
	}
	Return result
}


Post Reply

Return to “Ask for Help (v1)”