Get filepath of the file currently selected in Explorer
Get filepath of the file currently selected in Explorer
Hi guys, is there a way to do this with Windows 10 Explorer?
The flow is:
1. (Manually) Explorer is opened and a certain file is highlighted.
2. Hotkey is pressed (LShift in my case)
3. AHK script gets the absolute file path of the file
The flow is:
1. (Manually) Explorer is opened and a certain file is highlighted.
2. Hotkey is pressed (LShift in my case)
3. AHK script gets the absolute file path of the file
Re: Get filepath of the file currently selected in Explorer
try this:
Code: Select all
#HotIf WinActive("ahk_class CabinetWClass ahk_exe explorer.exe")
LShift::
{
items := ExplorerGetSelection()
MsgBox "selected items:`n" items
}
#HotIf
ExplorerGetSelection() {
if !hwnd := WinActive("ahk_class CabinetWClass ahk_exe explorer.exe")
return
try activeTab := ControlGetHwnd("ShellTabWindowClass1", hwnd)
for w in ComObject("Shell.Application").Windows {
if w.hwnd != hwnd
continue
if IsSet(activeTab) {
static IID_IShellBrowser := "{000214E2-0000-0000-C000-000000000046}"
shellBrowser := ComObjQuery(w, IID_IShellBrowser, IID_IShellBrowser)
ComCall(3, shellBrowser, "uint*", &thisTab:=0)
if thisTab != activeTab
continue
}
foundWin := w
break
}
str := ""
for i in foundWin.Document.SelectedItems
str .= i.Path "`n"
return str
}
Re: Get filepath of the file currently selected in Explorer
This uses @teadrinker’s Explorer_GetSelection(), modified for v2 (you posted in the v2 section).
Edit: Posted before seeing ntepa’s post.
Code: Select all
#Requires AutoHotkey v2.0
LShift::MsgBox Explorer_GetSelection()
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
;~ if !result
;~ result := shellFolderView.Folder.Self.Path
Return result
}
Edit: Posted before seeing ntepa’s post.
Re: Get filepath of the file currently selected in Explorer
Both scripts get Error: (0x8000FFFF) here. Windows 11.
This is the scripts I got from asking in the forum before: viewtopic.php?f=83&t=114431
This is the scripts I got from asking in the forum before: viewtopic.php?f=83&t=114431
Re: Get filepath of the file currently selected in Explorer
It worked here.
-
- Posts: 20
- Joined: 16 Feb 2023, 11:07
Re: Get filepath of the file currently selected in Explorer
A maybe more inferior approach, but simpler, is just to have your hotkey send a ctrl+c command which would both copy the file and the path, which is what youre after. You can put the A_Clipboard value into a variable and you can either keep the double quotes that are a part of the string, or remove them.
- flyingDman
- Posts: 2791
- Joined: 29 Sep 2013, 19:01
Re: Get filepath of the file currently selected in Explorer
Both scripts work here. You choice is whether you want all selected files returned - including in other than the active tab - or just those in the active tab ...
14.3 & 1.3.7
-
- Posts: 31
- Joined: 13 Aug 2021, 13:34
Re: Get filepath of the file currently selected in Explorer
For everyone looking for a quick fix:
In Windows 11, Ctrl+Shift+C would copy the entire path.
In Windows 11, Ctrl+Shift+C would copy the entire path.
Re: Get filepath of the file currently selected in Explorer
I went the context menu route (it's the first thing I tried just now):
It works, and it's so fast that the context menu isn't displayed on screen.
First command clicks Shift+Menu, second command "A" which is a shortcut for "Copy as path" menu option.
Thanks, ya'll!
Code: Select all
Enter::{
Send '+{AppsKey}'
Send 'A'
}
First command clicks Shift+Menu, second command "A" which is a shortcut for "Copy as path" menu option.
Thanks, ya'll!
Re: Get filepath of the file currently selected in Explorer
Implemented this solution
Thanks ya'll
Code: Select all
Enter::{
Send '+{AppsKey}' ; Click Shift+Menu
Send 'A' ; Click 'A' which is "copy as path" menu shortcut
}