2

My goal is to be able to 'right click' a file in file explorer, bring up the context menu and 'left click' to select an option from that menu.

My, so far unsuccessful idea, has been to use os.system to get into command prompt

from os import system

system(r'cd C:\Users\UserName\Documents && RIGHT CLICK SOME DOCUMENT AND SELECT AN OPTION')

But my idea falls short after the && in the above raw string. Where usually just putting a filename e.g. 'doc.txt' after the && would left click and open the file, I want to right click.

I realise that my solution so far is not strictly python related but an issue with the command line.

However I'm very open to and would be very appreciative of pointers towards a python module that might help.

(I'm talking about Windows 10 command line here)

Thanks!

jon_burns
  • 31
  • 2
  • 1
    On the Windows command line, `&&` means *and if successful then do...*. So if you put a filename after that, first the `cd` will be executed, and the next thing that will happen is the same as would happen if you typed the filename at a fresh prompt. Nothing to do with left clicks or right clicks. – BoarGules Apr 05 '18 at 13:46
  • This has nothing to do with Python - if you want to invoke a right-click menu option on a file you'll have to scan for that option in `HKEY_LOCAL_MACHINE\SOFTWARE\Classes\*\shellex\ContextMenuHandlers` and other appropriate `ContextMenuHandlers` entries in Windows Registry and then execute what's defined as that context menu command. – zwer Apr 05 '18 at 13:51
  • Cheers, you're right, nothing to do with python. In terms of the left click I didn't mean that `&&` invokes a left click, I meant that entering a filename eg `doc.txt` performs the equivalent of a left click. But I see it was ambiguous. – jon_burns Apr 05 '18 at 14:06
  • @zwer, if possible this should use the shell API instead of rummaging through the registry, e.g. [`AssocQueryString`](https://msdn.microsoft.com/en-us/library/bb773471). – Eryk Sun Apr 05 '18 at 21:10
  • @jon_burns - I thought you wanted to simulate an actual right-click context menu selection on a file. if you already know your command and know the file/dir path you want to pass it then it's as easy as `subprocess.call(["command", "path"])`. If you need it to be executed from a certain folder (as indicated with your `cd ...`) you can pass `cwd=r"C:\Users\UserName\Documents"` to the `subprocess.call()` command as well. – zwer Apr 05 '18 at 22:41
  • @zwer - I'll try to better explain. I have a file which I would like to run an executable on. Running the executable brings up a gui and usually you would enter the name of an input file into the gui and the executable should run, but the gui has a bug such that this doesn't work. The only way I can get this executable to run is to find my input (.T35) file in File explorer, right click on it to bring up the context menu and select 'Run [name of executable]'. So I'm looking to replicate this process. Perhaps the Windows Reg option would be best. Would you perhaps be able to provide an example? – jon_burns Apr 06 '18 at 22:48
  • I've tried simulating keystrokes to manipulate the file explorer gui using pyautogui, but I've found it quite messy. Especially if I want to run the executable on multiple files in parallel. An address to the 'Run [name of executable]' option in the context menu that could be entered in command prompt seems like it might be more convenient. But I'm struggling to actually do it. – jon_burns Apr 06 '18 at 22:53
  • @jon_burns - Since you're asking for one specific case, you should sift through your Windows Registry to find the actual context menu entry (search your registry for whatever shows up in the context menu), then follow the class IDs until you find the actual command - that's the command that you should execute when submitting a file to your app and it should work via `subprocess.call()` as well. – zwer Apr 06 '18 at 23:04
  • @zwer - Ok, I've found the location of the command. And so have tried... `subprocess.call([r'Computer\HKEY_CLASSES_ROOT\T35file\Shell\Run &Poisson\command', r'C:\Users\UserName\Documents\file.T35'])` But I am getting the error message `FileNotFoundError: [WinError 2] The system cannot find the file specified` – jon_burns Apr 06 '18 at 23:46
  • Ah, I’m wondering whether it might be to do with administrator permissions required for windows registry – jon_burns Apr 06 '18 at 23:56
  • Ok, I've figured it out. Thanks for your help! – jon_burns Apr 07 '18 at 10:52

0 Answers0