Recent

Author Topic: Explorer Context Menu -> perform action on multiple files  (Read 408 times)

domasz

  • Hero Member
  • *****
  • Posts: 546
Explorer Context Menu -> perform action on multiple files
« on: August 31, 2024, 02:03:25 pm »
Code: Pascal  [Select][+][-]
  1.     RootKey := HKEY_CLASSES_ROOT;
  2.  
  3.     APP_NAME := 'MyProg';
  4.  
  5.     if OpenKey('\*\shell\' + APP_NAME, true) then
  6.       WriteString('', 'Action with My Prog');
  7.     if OpenKey('\*\shell\' + APP_NAME + '\Command', true) then
  8.       WriteString('', '"' + Application.ExeName + '" c "%1"');  
  9.     if OpenKey('\*\shell\' + APP_NAME + '\Command', true) then
  10.       WriteString('MultiSelectModel', 'Player');

The above code adds an option to Explorer's context menu named "Action with My Prog".
When I invoke this action on a file- it opens my program and passes this file as a parameters. Works great.

But- "MultiSelectModel" should enable me to select multiple files in Explorer, invoke my action from context menu and pass all those files to a single instance of my program. But it opens as many copies of my program as files selected in Explorer.

How can I fix it? Without making .DLLs and using "Send To".

domasz

  • Hero Member
  • *****
  • Posts: 546
Re: Explorer Context Menu -> perform action on multiple files
« Reply #1 on: August 31, 2024, 04:52:20 pm »
I don't think it can be reliably done with just editing the Registry. A trick with single instance won't work for me.

So most likely I will just use code from the great CudaText:
https://github.com/dinkumoil/cuda_shell_extension

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1410
    • Lebeau Software
Re: Explorer Context Menu -> perform action on multiple files
« Reply #2 on: September 01, 2024, 02:32:18 am »
But- "MultiSelectModel" should enable me to select multiple files in Explorer, invoke my action from context menu and pass all those files to a single instance of my program. But it opens as many copies of my program as files selected in Explorer.

If you read the MSDN documentation: How to Employ the Verb Selection Model, you would see that you are registering the "MultiSelectModel" value in the wrong Registry key.  It belongs in the verb key, not in the verb's "Command" key.

Also, per other MSDN documentation: HKEY_CLASSES_ROOT key, you should NOT write directly to HKEY_CLASSES_ROOT at all.  Write to either "HKEY_LOCAL_MACHINE\Software\Classes" (all users) or "HKEY_CURRENT_USER\Software\Classes" (current user) instead.

Try this:

Code: Pascal  [Select][+][-]
  1. RootKey := HKEY_CURRENT_USER; // or HKEY_LOCAL_MACHINE
  2.  
  3. APP_NAME := 'MyProg';
  4.  
  5. if OpenKey('\Software\Classes\*\shell\' + APP_NAME, true) then
  6. begin
  7.   WriteString('', 'Action with My Prog');
  8.   WriteString('MultiSelectModel', 'Player');
  9.   if OpenKey('Command', true) then
  10.     WriteString('', '"' + Application.ExeName + '" c "%1"');  
  11. end;

Now, that being said, this will still not do what you want.  It will still invoke your "command" separately for each selected file, thus invoking your app multiple times.  That is simple enough to handle, by having subsequent instances of your app send each file to an existing instance.  But that is not efficient.

When I search my Registry, every use of "MultiSelectModel" uses either "ExplorerCommandHandler" or "DelegateExecute" to actually handle their commands:

- For "ExplorerCommandHandler", you need to write a COM server that implements the IExplorerCommand interface, whose Invoke() method receives multiple files as an IShellItemArray.

- For "DelegateExecute", you need to write a COM server that implements the IExecuteCommand and IObjectWithSelection interfaces. The IObjectWithSelection::SetSelection() method receives multiple files as an IShellItemArray, and then IExecuteCommand::Execute() acts on them.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

domasz

  • Hero Member
  • *****
  • Posts: 546
Re: Explorer Context Menu -> perform action on multiple files
« Reply #3 on: September 01, 2024, 12:22:50 pm »
Thanks, Remy!

 

TinyPortal © 2005-2018