Shortcut to copy path of selected file in Explorer Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
JohnTravolski
Posts: 21
Joined: 22 Dec 2015, 15:10

Shortcut to copy path of selected file in Explorer

10 Jul 2018, 02:16

I'm not sure if this is possible, but I'm wondering if AutoHotKey can be used to create a keyboard shortcut that, when inside File Explorer, copies the path of the highlighted item to the clipboard if a file or folder is selected, and copies the path to the current folder being browsed in if nothing is selected. I've done some research, and it seems like something along those lines can be done, as seen here, but unfortunately I don't really understand it:

https://autohotkey.com/board/topic/6072 ... ile/page-2

For example, if I'm at the following directory in File Explorer (C:\Expedited\Scripts\AutoHotKey) and I have the item "OpenWith.ahk" selected (highlighted in blue in File Explorer), then the text "C:\Expedited\Scripts\AutoHotKey\OpenWith.ahk" should be copied to the clipboard. If no item was selected, then just the text "C:\Expedited\Scripts\AutoHotKey" should be copied.

However, I'm a relatively new user and I don't quite understand how it works. With something like this being pretty useful to just about anybody, I also wouldn't be surprised if a script like this has already been written by somebody out there. Do you know of such a script? If not, can it be done in AutoHotKey?
Guest

Re: Shortcut to copy path of selected file in Explorer

10 Jul 2018, 05:17

Simply sending ^c will copy the path of the selected file(s) to the clipboard as text which you can simply access using the Clipboard variable.

See https://autohotkey.com/docs/misc/Clipboard.htm - second paragraph.

Various techniques here https://autohotkey.com/boards/viewtopic.php?t=11297 incl clipboard but also using COM (similar to what you've linked too) - try several, choose the one that work for you :-)
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Shortcut to copy path of selected file in Explorer  Topic is solved

13 Jul 2018, 07:39

- There is always one item (file/folder) that is focused. The item is either selected (highlighted blue) or it is not selected.
- I would suppose that the best approach would be to retrieve the path of the focused item if it is selected, or otherwise, to retrieve the path of the folder if the focused item is not selected.
- In the script I've made it so that the script sends Ctrl+C when the main Explorer control (DirectUIHWND3) is not focused.
- It's easy to retrieve the focused item using a COM object, but AFAIK it is not so easy to get an 'IsSelected' state for that item. The script compares the name of the focused item against all selected items to determine if it is selected. Cheers.

Code: Select all

#IfWinActive, ahk_class CabinetWClass
q:: ;Explorer window - copy focused item path (if item selected) else folder path
#IfWinActive, ahk_class ExploreWClass
q:: ;Explorer window - copy focused item path (if item selected) else folder path
WinGet, hWnd, ID, A
ControlGetFocus, vCtlClassNN, % "ahk_id " hWnd
if !(vCtlClassNN = "DirectUIHWND3")
{
	SendInput, ^c
	return
}

vPath := ""
for oWin in ComObjCreate("Shell.Application").Windows
	if (oWin.HWND = hWnd)
	{
		vPath := oWin.Document.FocusedItem.Path
		if (vPath = "")
			vPath := oWin.Document.Folder.Self.Path
		else
		{
			vIsSelected := 0
			for oItem in oWin.Document.SelectedItems
				if (vPath = oItem.Path)
				{
					vIsSelected := 1
					break
				}
			if !vIsSelected
				vPath := oWin.Document.Folder.Self.Path
		}
		break
	}
oWin := oItem := ""
Clipboard := vPath
MsgBox, % vPath
return
#IfWinActive
- Link:
FolderItem object | Microsoft Docs
https://docs.microsoft.com/en-gb/window ... folderitem
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
JohnTravolski2

Re: Shortcut to copy path of selected file in Explorer

14 Jul 2018, 01:40

jeeswg wrote:- There is always one item (file/folder) that is focused. The item is either selected (highlighted blue) or it is not selected.
- I would suppose that the best approach would be to retrieve the path of the focused item if it is selected, or otherwise, to retrieve the path of the folder if the focused item is not selected.
- In the script I've made it so that the script sends Ctrl+C when the main Explorer control (DirectUIHWND3) is not focused.
- It's easy to retrieve the focused item using a COM object, but AFAIK it is not so easy to get an 'IsSelected' state for that item. The script compares the name of the focused item against all selected items to determine if it is selected. Cheers.

Code: Select all

#IfWinActive, ahk_class CabinetWClass
q:: ;Explorer window - copy focused item path (if item selected) else folder path
#IfWinActive, ahk_class ExploreWClass
q:: ;Explorer window - copy focused item path (if item selected) else folder path
WinGet, hWnd, ID, A
ControlGetFocus, vCtlClassNN, % "ahk_id " hWnd
if !(vCtlClassNN = "DirectUIHWND3")
{
	SendInput, ^c
	return
}

vPath := ""
for oWin in ComObjCreate("Shell.Application").Windows
	if (oWin.HWND = hWnd)
	{
		vPath := oWin.Document.FocusedItem.Path
		if (vPath = "")
			vPath := oWin.Document.Folder.Self.Path
		else
		{
			vIsSelected := 0
			for oItem in oWin.Document.SelectedItems
				if (vPath = oItem.Path)
				{
					vIsSelected := 1
					break
				}
			if !vIsSelected
				vPath := oWin.Document.Folder.Self.Path
		}
		break
	}
oWin := oItem := ""
Clipboard := vPath
MsgBox, % vPath
return
#IfWinActive
- Link:
FolderItem object | Microsoft Docs
https://docs.microsoft.com/en-gb/window ... folderitem

This functions perfectly, thank you very much!
PiesHelpfulOven
Posts: 2
Joined: 12 Oct 2023, 17:18

Re: Shortcut to copy path of selected file in Explorer

19 Mar 2024, 22:12

jeeswg wrote:
13 Jul 2018, 07:39
- There is always one item (file/folder) that is focused. The item is either selected (highlighted blue) or it is not selected.
- I would suppose that the best approach would be to retrieve the path of the focused item if it is selected, or otherwise, to retrieve the path of the folder if the focused item is not selected.
- In the script I've made it so that the script sends Ctrl+C when the main Explorer control (DirectUIHWND3) is not focused.
- It's easy to retrieve the focused item using a COM object, but AFAIK it is not so easy to get an 'IsSelected' state for that item. The script compares the name of the focused item against all selected items to determine if it is selected. Cheers.

Code: Select all

#IfWinActive, ahk_class CabinetWClass
q:: ;Explorer window - copy focused item path (if item selected) else folder path
#IfWinActive, ahk_class ExploreWClass
q:: ;Explorer window - copy focused item path (if item selected) else folder path
WinGet, hWnd, ID, A
ControlGetFocus, vCtlClassNN, % "ahk_id " hWnd
if !(vCtlClassNN = "DirectUIHWND3")
{
	SendInput, ^c
	return
}

vPath := ""
for oWin in ComObjCreate("Shell.Application").Windows
	if (oWin.HWND = hWnd)
	{
		vPath := oWin.Document.FocusedItem.Path
		if (vPath = "")
			vPath := oWin.Document.Folder.Self.Path
		else
		{
			vIsSelected := 0
			for oItem in oWin.Document.SelectedItems
				if (vPath = oItem.Path)
				{
					vIsSelected := 1
					break
				}
			if !vIsSelected
				vPath := oWin.Document.Folder.Self.Path
		}
		break
	}
oWin := oItem := ""
Clipboard := vPath
MsgBox, % vPath
return
#IfWinActive
- Link:
FolderItem object | Microsoft Docs
https://docs.microsoft.com/en-gb/windows/desktop/shell/folderitem
Do you have a version of this in autohotkey v2?
GEV
Posts: 1002
Joined: 25 Feb 2014, 00:50

Re: Shortcut to copy path of selected file in Explorer

20 Mar 2024, 12:39

PiesHelpfulOven wrote:
19 Mar 2024, 22:12
Do you have a version of this in autohotkey v2?
You can try this

Code: Select all

#Requires AutoHotkey v2.0

; Copy the path of selected items in Explorer

#HotIf WinActive("ahk_class CabinetWClass")

	q:: A_Clipboard := Explorer_GetSelection()

#HotIf

; https://www.autohotkey.com/boards/viewtopic.php?f=82&p=519087#p514264
Explorer_GetSelection() {
   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")  ; 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
   Return result
}

It doesn't work on Desktop (#HotIf WinActive("ahk_class CabinetWClass") || WinActive("ahk_class Progman"))
Can someone correct it?
GEV
Posts: 1002
Joined: 25 Feb 2014, 00:50

Re: Shortcut to copy path of selected file in Explorer

25 Mar 2024, 15:32

This seems to work both in explorer and on the desktop:

Code: Select all

#Requires AutoHotkey v2.0

; Copy the path of selected items in explorer or on the desktop
#HotIf WinActive("ahk_class CabinetWClass") || WinActive("ahk_class Progman")

	q:: A_Clipboard := 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
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee and 313 guests