Selecting a context menu item by text value

Get help with using AutoHotkey and its commands and hotkeys
dheylen_2000
Posts: 5
Joined: 09 Dec 2017, 19:24

Selecting a context menu item by text value

09 Dec 2017, 19:39

I'm trying to create a hotkey to combine multiple selected pdf files into a single pdf file through the AppsKey context menu.

My only problem is that sometimes the position in the context menu for this option ("Combine supported files in Acrobat...") is the 3rd item down, sometimes it's the 6th item down, sometimes it's the 8th item down, etc. I can't find any rhyme or reason for why this position keeps changing.

Is there any way for AutoHotKey to select a context item by the text value rather than the position? I have seen Misha's script but my programming skills aren't nearly strong enough and I don't have enough time to read through all the other programming to parse out what I need to do. Are there any scripts or any directions that just do this one thing?

Thanks for any help
User avatar
Barney
Posts: 54
Joined: 28 May 2014, 20:03
Location: Germany

Re: Selecting a context menu item by text value

10 Dec 2017, 06:11

try typing the letters of the text. If it works you can simulate the keystrokes with autohotkey.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Selecting a context menu item by text value

10 Dec 2017, 06:42

Does the item appear somewhere as a menu item on a context menu descended from the menu bar? Does WinMenuSelectItem work?

Do you mean this script by Micha (that someone updated):
Get Info from Context Menu (x64/x32 compatible) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=31971

The principle of invoking menu items is to create a one-liner like this.
SendMessage, 0x111, 3,,, % "ahk_id " hWnd ;WM_COMMAND := 0x111
The menu item ID might not be 3, Micha's script shows a ToolTip with menu item IDs; usually, but not always that number is the number you need.

To literally do what you requested, this script should work:

Code: Select all

;based on:
;GUIs via DllCall: get/set internal/external control text - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=40514
;context menu window messages: focus/invoke item - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=5&t=39209

q:: ;invoke menu item in context menu that matches string
;e.g. Notepad right-click menu, 'Paste' menu item
SendMessage, 0x1E1,,,, ahk_class #32768 ;MN_GETHMENU := 0x1E1
hMenu := ErrorLevel

vNeedle := "Paste"
vNeedle := StrReplace(vNeedle, "&")
Loop, % DllCall("user32\GetMenuItemCount", Ptr,hMenu)
{
	vIndex := A_Index-1
	vID := DllCall("user32\GetMenuItemID", Ptr,hMenu, Int,vIndex, UInt)
	if (vID = 0) || (vID = 0xFFFFFFFF) ;-1
		continue
	vChars := DllCall("user32\GetMenuString", Ptr,hMenu, UInt,vIndex, Ptr,0, Int,0, UInt,0x400) + 1
	VarSetCapacity(vText, vChars << !!A_IsUnicode)
	DllCall("user32\GetMenuString", Ptr,hMenu, UInt,vIndex, Str,vText, Int,vChars, UInt,0x400) ;MF_BYPOSITION := 0x400
	if (StrReplace(vText, "&") = vNeedle)
	{
		PostMessage, 0x1F1, % vIndex, 0,, ahk_class #32768 ;MN_DBLCLK := 0x1F1
		break
	}
}
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
BoBo
Posts: 4333
Joined: 13 May 2014, 17:15

Re: Selecting a context menu item by text value

10 Dec 2017, 06:53

jeeswg wrote:Does the item appear somewhere as a menu item on a context menu descended from the menu bar? Does WinMenuSelectItem work?

Do you mean this script by Micha (that someone updated):
Get Info from Context Menu (x64/x32 compatible) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=31971

The principle of invoking menu items is to create a one-liner like this.
SendMessage, 0x111, 3,,, % "ahk_id " hWnd ;WM_COMMAND := 0x111
The menu item ID might not be 3, Micha's script shows a ToolTip with menu item IDs; usually, but not always that number is the number you need.

To literally do what you requested, this script should work: [...]
Tested (W7 Pro 64, Notepad) worked (for me)! :clap:

Return to “Ask For Help”

Who is online

Users browsing this forum: AHKStudent, asundrus, Bing [Bot], Google [Bot], Ioniq, superpeter, tomjtoth and 37 guests