Get command associated with context menu items?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
andymbody
Posts: 934
Joined: 02 Jul 2017, 23:47

Get command associated with context menu items?

Post by andymbody » 04 May 2024, 16:47

Does anyone know how to get the underlying command that is associated with a context menu item? I can get the name of the item using GetMenuString, but I would like to get the command that will be executed if that item is clicked. So basically the registry entry that is associated with that particular menu item.

For instance, a text document might have the command 'notepad.exe %1' associated with 'open' of .txt files. I am not interested in notepad or any other common commands. The items I am interested in are unique commands.

Background...

I am working with a user who has requested a script to automate tasks on their computer. These tasks require frequent context menu access to run certain commands. This frequent menu access prevents the user from using the computer while the script is running. So I have asked them to get the commands that are associated with the menu items so that they can be executed in the background without frequent menu popups.

The user is not comfortable with digging into the registry to find/extract the commands, so I am trying to get these commands another way. My thought is to write a script that they can run to get the commands required. Then forward that info to me, so I can use 'Run' instead of opening context menus. But The only info I currently have is the name and positions of those menu items. The names are unique, I just need a way to search the registry remotely, looking for the commands associated with the target context menu items. I do not have direct access to the users computer. I write the script, they inspect and run it. They do not wish to allow direct access to anyone, including me. They are very safety conscious which is understandable, as am I.

As of now, we are talking about 3 menu items, but this might change in future script updates.

I can start looking in HKEY_CLASSES_ROOT\*\shell\, HKEY_CLASSES_ROOT\.ext and HKEY_CLASSES_ROOT\APP\Shell\. But I think the last 2 will not be related to context menus.

Any other suggestions or related scripts?

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

Re: Get command associated with context menu items?

Post by teadrinker » 05 May 2024, 08:05

When the user clicks a menu item, a WM_COMMAND message is sent to the application window with the item ID in wParam. You can simulate a click on this menu item by sending a corresponding message to the window. The easiest way to find out the ID of a menu item is to use applications that intercept messages (Spy++), but you can do it programmatically as well.

User avatar
andymbody
Posts: 934
Joined: 02 Jul 2017, 23:47

Re: Get command associated with context menu items?

Post by andymbody » 05 May 2024, 11:50

teadrinker wrote:
05 May 2024, 08:05
When the user clicks a menu item, a WM_COMMAND message is sent to the application window with the item ID in wParam.
Thank you for the suggestion teadrinker. I value your input.

Correct me if I am wrong. I think this would be the case if an external app window was already open/running and the menu was part of that app, correct? In this case, the app is not running when the menu item is 'clicked'.

Currently the operation looks like this....

1. Multiple folders are added by the user to a listView within the script (up to 15 folders).
2. Each folder contains up to 200 files of a particular extension (for a total of up to 3000 files), but there could be other files within same folder as well (of other extensions).
3. The script selects the 200 target files within one of the folders, then simulates right-click (Shift-F10), arrows down to target menu item, then simulates 'enter'.
4. This causes an external app (that is not already running) to act upon each of these 200 files, one at a time.
5. The external app creates up to 3 more files for each of the 200 (800 total now), and 400 of the additional files (600 total) must be processed using steps 1-4 again (using different context menu selections).
6. This process is repeated for each of the folders in step 1 (for a total of up to 12000 files in a single session). There are other details that are also taking place during this process, that are not mentioned here (for simplicity).
7. The script monitors this entire process by indirectly monitoring clues of the operation performed by the external app (one of which is the creation of the additional files, but there are many more taking place at the same time).

The current script works flawlessly, but step 3 (which is performed for each file selection of up to 200) prevents the user from using the computer for any other purpose, since the popup menu cannot be interrupted without breaking the sequence of events. A single session can take hours, which locks the computer into a single function for that time. And they would like to use the computer for other tasks while this process is taking place 'in the background'.

So, I would like the script to use Run to call the external app (for each of the files - one file at a time) in the background. Rather than multi-select, and using a disruptive context menu selection to do the same.

The context menu selection initiates the external app, which then closes after each file is processed. So no persistent app can receive the command that you have suggested. It would need to be running prior to the call that the menu item is performing. So I'm looking for the "command line" equivalent, which will initiate the external app, and that command is probably found in the registry.

Does this make sense? Am I asking the right question?
Andy
Last edited by andymbody on 05 May 2024, 12:18, edited 2 times in total.

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

Re: Get command associated with context menu items?

Post by teadrinker » 05 May 2024, 12:15

andymbody wrote: So no persistent app can receive the command that you have suggested.
If a context menu can be called, then there is an application that displays it. When the user clicks on a menu item, the application that opened the menu receives a WM_COMMAND message and executes the command associated with that menu item.

User avatar
andymbody
Posts: 934
Joined: 02 Jul 2017, 23:47

Re: Get command associated with context menu items?

Post by andymbody » 05 May 2024, 12:26

teadrinker wrote:
05 May 2024, 12:15
If a context menu can be called, then there is an application that displays it. When the user clicks on a menu item, the application that opened the menu receives a WM_COMMAND message and executes the command associated with that menu item.
Hmmm... ok, so if I understand correctly... the menu is displayed by Win Explorer... so Win Explorer would be the window that receives the WM_COMMAND message from the "menu click" and acts upon it (calling the external app)? So if I can spy on that communication, I can identify the command that can be used to 'simulate' the menu click, without actually displaying the menu itself? But how would I use that WM_COMMAND command from the script to call the external app without actually opening Win Explorer? Would Win Explorer be required to be open in this calling process?

Ideally, I'd like to use Run to do something like this (the syntax might not be perfect here).
Run externalapp.exe '"' currentFile '"' ; including any required external app switches

But if this is not going to be possible, I can see if the WM_COMMAND idea is less disruptive than the current process (once I figure out the details... :D )

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

Re: Get command associated with context menu items?

Post by teadrinker » 05 May 2024, 13:05

andymbody wrote: I can identify the command that can be used to 'simulate' the menu click, without actually displaying the menu itself?
I wanted to reply that it is, but I decided to test it first. Unfortunately, it turned out that approach doesn't work with Windows Explorer.
andymbody wrote: My thought is to write a script that they can run to get the commands required.
I'm not sure about the possibility of implementing this. Commands corresponding to some menu items are implicitly written in the registry.

User avatar
Seven0528
Posts: 392
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: Get command associated with context menu items?

Post by Seven0528 » 05 May 2024, 13:12

 InvokeVerb()
Even though I don't fully understand what you want to do...
Would the document above be helpful?
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.

User avatar
andymbody
Posts: 934
Joined: 02 Jul 2017, 23:47

Re: Get command associated with context menu items?

Post by andymbody » 05 May 2024, 13:30

teadrinker wrote:
05 May 2024, 13:05
I wanted to reply that it is, but I decided to test it first. Unfortunately, it turned out that approach doesn't work with Windows Explorer.
I tried this just now with opening a simple .txt file with WinExplorer and the little spy app (I use sometimes) didn't record any comms. Thank you for letting me know that your test failed as well. Saves me from trying to write a more detailed spy that would have failed also.

I'm not sure about the possibility of implementing this. Commands corresponding to some menu items are implicitly written in the registry.
Yeah, I'm sure it's not trivial to get this info from the registry. I have never tried before.

I'm thinking they better just dedicate a computer to this operation because it is quite complex with multiple layers and logical dimensions (I left out a lot of details for simplicity purposes). He has informed me that they will probably run this operation each night while they sleep, so the interactions may be minimal if that is implemented as a rule. If this is not the case, I think it will require more diagnostic participation from the client to design a less obtrusive solution.

Thank you for you valuable input!
Last edited by andymbody on 05 May 2024, 13:42, edited 1 time in total.

User avatar
andymbody
Posts: 934
Joined: 02 Jul 2017, 23:47

Re: Get command associated with context menu items?

Post by andymbody » 05 May 2024, 13:37

Seven0528 wrote:
05 May 2024, 13:12
Even though I don't fully understand what you want to do...
You and me both... lol
 InvokeVerb()
Would the document above be helpful?
Thank you! I was actually playing with that (or maybe similar post) last night. Lexikos is always helpful with more complex solutions. I will look at this post to see if it's the same post I played with. He had posted a small script that grabbed the 'verbs' that were associated with .ext files. I had no idea this could be extracted so 'easily' (he makes it look easy). This might be the same info, I'll have to look in a minute.

Post Reply

Return to “Ask for Help (v2)”