Explorer: get name of file under cursor

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Explorer: get name of file under cursor

10 Jul 2018, 00:21

- In response to this:
[x64 & x32 fix] DeskIcons - Get/Set Desktop Icon Positions - Page 2 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 74#p227874

- Here's a way to get the name of the file under the cursor via Acc:

Code: Select all

;[Acc functions]
;Acc library (MSAA) and AccViewer download links - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=26201

;tested on Windows 7
q:: ;Explorer window/Desktop - get name of file under cursor
oAcc := Acc_ObjectFromPoint(vChildID)
MouseGetPos,,, hWnd, vCtlClassNN
WinGetClass, vWinClass, % "ahk_id " hWnd
vText := ""
if (vWinClass = "CabinetWClass") || (vWinClass = "ExploreWClass")
	try vText := oAcc.accValue(vChildID)
else if (vWinClass = "Progman") || (vWinClass = "WorkerW")
	try vText := oAcc.accName(vChildID)
MsgBox, % vText
oAcc := ""
return
- If you experiment with AccViewer, you can get information for, and see the boundaries of, different GUI windows/controls/elements under the cursor.
Acc library (MSAA) and AccViewer download links - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=26201

- Also of interest:
.net - c# how to get window explorer Directory path under mouse cursor - Stack Overflow
https://stackoverflow.com/questions/171 ... use-cursor
c# - Get the folder path under cursor? - Stack Overflow
https://stackoverflow.com/questions/786 ... der-cursor
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
r2997790
Posts: 71
Joined: 02 Feb 2017, 02:46

Re: Explorer: get name of file under cursor

10 Jul 2018, 23:37

Hi Jesswg,

I was wondering if it was possible to retrieve (in real time) the menu text under a menu?

eg. You press File > Open (and the ACC script would return 'Open' as that is the text under the mouse?

I've had a look at ACC Viewer but it's not possible to have a menu open to interrogate it while using the tool?

Do you think it is possible? Thanks!

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

Re: Explorer: get name of file under cursor

10 Jul 2018, 23:48

- Try this to get the text from the GUI element under the cursor.

Code: Select all

;[Acc functions]
;Acc library (MSAA) and AccViewer download links - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=26201

q:: ;get text from GUI element under cursor
oAcc := Acc_ObjectFromPoint(vChildID)
;vChildID := 0 ;for a slightly different result uncomment this line
vText1 := vText2 := ""
try vText1 := oAcc.accName(vChildID) ;get text
try vText2 := oAcc.accValue(vChildID) ;get value
if !(vText1 = "")
	Clipboard .= "`r`n" vText1
if !(vText2 = "") && !(vText = vText2)
	Clipboard .= "`r`n" vText2
oAcc := ""
return
- Btw you can use Ctrl+/ to focus a GUI element with AccViewer.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
r2997790
Posts: 71
Joined: 02 Feb 2017, 02:46

Re: Explorer: get name of file under cursor

10 Jul 2018, 23:58

Thank you... it works... wonderful!
r2997790
Posts: 71
Joined: 02 Feb 2017, 02:46

Re: Explorer: get name of file under cursor

11 Jul 2018, 00:59

Jesswg, I've had a play with this and it works pretty well... Thank you.

Is it possible to detect the menu state and only run it to detect menu items when the menu is open?

I also had some trouble getting it to work with Photoshop... seems a bit hit and miss... is there any tips/tricks to get it to be more stable with Adobe? Or perhaps they block/don't expose the menu information? I was working intermittently. Odd.

Thank you for your insights.. super helpful.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Explorer: get name of file under cursor

11 Jul 2018, 01:06

- Add these lines at the top between 'q::' and 'oAcc :='.

Code: Select all

DetectHiddenWindows, Off
if !WinExist("ahk_class #32768")
	return
- This will end the subroutine early if no context menu window is detected.
- For Photoshop, which I don't use, I would recommend investigating with AccViewer, try to do the same thing a few times, and look for patterns for when it does/doesn't work.
- Also, try using the ToolTip command to inform you of what's happening, if using the script above.
- No problem, glad they're helpful.

- [EDIT:] Also, check the class of the menu by using MouseGetPos when the cursor is over the menu, the menu may be nonstandard, i.e. not of class #32768.

Code: Select all

MouseGetPos, vCurX, vCurY, hWnd, vCtlClassNN
WinGetClass, vWinClass, % "ahk_id " hWnd
MsgBox, % vWinClass
- Also, perhaps a menu 'item' is made up of multiple parts (GUI elements), you can check the boundaries with AccViewer.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
r2997790
Posts: 71
Joined: 02 Feb 2017, 02:46

Re: Explorer: get name of file under cursor

11 Jul 2018, 05:21

Thank you very much for this feedback... the use case I'm looking to implement is a 'real-time' text translation of menus.

Unfortunately I often have to us apps which don't have english as the first language (but which I am broadly familiar with) and I want to be able to display a (translated) tooltip of the menu item I am currently on top of.

Your input will help a lot.

Thank you.
godofOOF
Posts: 27
Joined: 22 Dec 2018, 06:03

Re: Explorer: get name of file under cursor

22 Dec 2018, 07:18

How can i copy the names under the cursor?
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Explorer: get name of file under cursor

24 Dec 2018, 00:17

- There can only be one name under the cursor. You can use the special Clipboard variable.
- What you want might be possible, if you can be more specific.

'There can be only one.' Campione.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
godofOOF
Posts: 27
Joined: 22 Dec 2018, 06:03

Re: Explorer: get name of file under cursor

24 Dec 2018, 00:54

jeeswg wrote:
24 Dec 2018, 00:17
- There can only be one name under the cursor. You can use the special Clipboard variable.
- What you want might be possible, if you can be more specific.

'There can be only one.' Campione.

- How do i get the same information or folder name
- the msgbox gives you based off the example script above
- copied on a clipboard
- instead of presented on a msgbox
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Explorer: get name of file under cursor

24 Dec 2018, 00:58

The special Clipboard variable.

Code: Select all

Clipboard := vText
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
godofOOF
Posts: 27
Joined: 22 Dec 2018, 06:03

Re: Explorer: get name of file under cursor

24 Dec 2018, 01:07

jeeswg wrote:
24 Dec 2018, 00:58
The special Clipboard variable.

Code: Select all

Clipboard := vText
Thanks man i appreciate it :dance:
iPhilip
Posts: 814
Joined: 02 Oct 2013, 12:21

Re: Explorer: get name of file under cursor

21 Feb 2019, 14:43

jeeswg wrote:
10 Jul 2018, 00:21
- In response to this:
[x64 & x32 fix] DeskIcons - Get/Set Desktop Icon Positions - Page 2 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=3529&p=227874#p227874

- Here's a way to get the name of the file under the cursor via Acc:

Code: Select all

;[Acc functions]
;Acc library (MSAA) and AccViewer download links - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=26201

;tested on Windows 7
q:: ;Explorer window/Desktop - get name of file under cursor
oAcc := Acc_ObjectFromPoint(vChildID)
MouseGetPos,,, hWnd, vCtlClassNN
WinGetClass, vWinClass, % "ahk_id " hWnd
vText := ""
if (vWinClass = "CabinetWClass") || (vWinClass = "ExploreWClass")
	try vText := oAcc.accValue(vChildID)
else if (vWinClass = "Progman") || (vWinClass = "WorkerW")
	try vText := oAcc.accName(vChildID)
MsgBox, % vText
oAcc := ""
return
Hi jeeswg,

Thank you for your post. Below is a version that returns the name of the icon regardless of where the mouse is positioned over it. This is especially important in configuratitons where CurrentViewMode > 1 [Ref], i.e. any configuration other than Medium, Large, and Extra Large Icons. Note that the value returned is not necessarily the name of the file as the post heading indicates. Consider, for example, shortcuts, which can be renamed, and the case where the "Hide extensions for known file types" setting is enabled.

Code: Select all

GetNameOfIconUnderMouse() {
   MouseGetPos, , , hwnd, CtrlClass
   WinGetClass, WinClass, ahk_id %hwnd%
   try if (WinClass = "CabinetWClass" && CtrlClass = "DirectUIHWND3") {
      oAcc := Acc_ObjectFromPoint()
      Name := Acc_Parent(oAcc).accValue(0)
      Name := Name ? Name : oAcc.accValue(0)
   } else if (WinClass = "Progman" || WinClass = "WorkerW") {
      oAcc := Acc_ObjectFromPoint(ChildID)
      Name := ChildID ? oAcc.accName(ChildID) : ""
   }
   Return Name
}
Here is a self-contained example (including only the required Acc functions):

Code: Select all

#NoEnv

SetTimer, Timer
Return

Timer:
ToolTip % GetNameOfIconUnderMouse()
Return

Esc::ExitApp

GetNameOfIconUnderMouse() {
   MouseGetPos, , , hwnd, CtrlClass
   WinGetClass, WinClass, ahk_id %hwnd%
   try if (WinClass = "CabinetWClass" && CtrlClass = "DirectUIHWND3") {
      oAcc := Acc_ObjectFromPoint()
      Name := Acc_Parent(oAcc).accValue(0)
      Name := Name ? Name : oAcc.accValue(0)
   } else if (WinClass = "Progman" || WinClass = "WorkerW") {
      oAcc := Acc_ObjectFromPoint(ChildID)
      Name := ChildID ? oAcc.accName(ChildID) : ""
   }
   Return Name
}

; https://github.com/Drugoy/Autohotkey-scripts-.ahk/blob/master/Libraries/Acc.ahk

Acc_Init() {
	Static h
	If Not h
		h:=DllCall("LoadLibrary","Str","oleacc","Ptr")
}
Acc_ObjectFromPoint(ByRef _idChild_ = "", x = "", y = "") {
	Acc_Init()
	If	DllCall("oleacc\AccessibleObjectFromPoint", "Int64", x==""||y==""?0*DllCall("GetCursorPos","Int64*",pt)+pt:x&0xFFFFFFFF|y<<32, "Ptr*", pacc, "Ptr", VarSetCapacity(varChild,8+2*A_PtrSize,0)*0+&varChild)=0
	Return ComObjEnwrap(9,pacc,1), _idChild_:=NumGet(varChild,8,"UInt")
}
Acc_Parent(Acc) { 
	try parent:=Acc.accParent
	return parent?Acc_Query(parent):
}
Acc_Query(Acc) { ; thanks Lexikos - www.autohotkey.com/forum/viewtopic.php?t=81731&p=509530#509530
	try return ComObj(9, ComObjQuery(Acc,"{618736e0-3c3d-11cf-810c-00aa00389b71}"), 1)
}
I hope this helps.

- iPhilip
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 70 guests