0

I need to run a right click windows context item and have the script being run pick up the url of active explorer window to be passed into script execution.

I have already created entries and updated the registry with a .bat and can make the script work if the user copies the address of the active window to the clipboard and then right clicks inside of the active folder and chooses the item from the menu. From research I have done, I do not seem to be able to find a way to do what I want with Python so far. I thought I might use psutils, but this has not proven useful, but I believe this may be due to out of date module, which I am updating.

I expect the user to just be able to right click in the folder and run the script without having to copy the explorer address to the clipboard first, so any pointers towards a better way to get the url will be welcome.

PyHulk
  • 56
  • 2
  • 8

2 Answers2

0

As you already mentioned, you need to add an entry to the Registry. Refer to this answer. Set the Registry value to "path/to/executable.exe" "%v". Make sure to include the "%v".

Then when the user clicks the context menu entry, your executable will be called with the path as a command line argument. You can then retrieve the path via sys.argv:

import sys
path = sys.argv[1]
jfhr
  • 687
  • 5
  • 13
  • I am not sure this is right but as I cant post code into the comment, I may need to use more than one. The code for the bat script is in next comment, and when I add the "%v" it adds, but sys.argv[1] does not work. Python is on one drive, so is the python script that will be run from the context menu and I want this to work on any folder in any drive. The python script as it is, does what I need if I supply it with clipboard text. The .bat script adds two a reference to python for the icon and a command key that has the path to python and the python script. It is here that I have added the %v. – PyHulk Apr 15 '19 at 11:33
  • What is the exact value of sys.argv? Also look at the other values in the array, you might need to use sys.argv[2] or something instead. – jfhr Apr 15 '19 at 12:00
  • The value of sys.argv[0] is errored with out of index, sys.argv[1] produces the name of the .py script. I will try some others, see what index I can use, if any. Thanks – PyHulk Apr 15 '19 at 13:17
  • I dont think that sys.argv is going to help. Please see my other answer to see if it clarifies things better as I am not sure the issue is understood in the manner I need it to be. The python script is NOT in the folder that I right click in. – PyHulk Apr 15 '19 at 13:23
0

Here is the code for the bat script, which is possibly where I need to update it further, but the version of the .bat where I do not have the %v worked if I copied the url of the Explorer window to the clipboard first as the python script that is run from the context picks that up.

@echo off
REG ADD "HKCR\Directory\Background\shell\Sort Product Renders" /f /v "Icon" /t REG_SZ /d "Z:\pipeline\Python\python.exe" 
REG ADD "HKCR\Directory\Background\shell\Sort Product Renders\command" /f /ve /t REG_SZ /d "Z:\pipeline\Python\python.exe %v Z:\sort_product_\sort_renders.py"

Following Jakobs advice, I have updated this to the above, run it, but it does not work.

this is what I had originally:

REG ADD "HKCR\Directory\Background\shell\Sort Product Renders\command" /f /ve /t REG_SZ /d "Z:\pipeline\Python\python.exe Z:\sort_product_\sort_renders.py"

I also tried:

REG ADD "HKCR\Directory\Background\shell\Sort Product Renders\command" /f /ve /t REG_SZ /d "Z:\pipeline\Python\python.exe %v"

If I print out the sys.argv[1], its the name of the file being run, which is not what is wanted. Our python .exe is on one server, as is the python script being run and I want a user to be able to go to any folder, right click, choose the python script and have the url of the open window passed to the script. So far, using jakobs info, this has not been successful.

I tried sys,argv[1] and it said index out of range, sys.argv[1] does not give the right response either.

PyHulk
  • 56
  • 2
  • 8