0

I wrote a python script and then convert it into executable file. In below image you can see my exe file. my exe file.

Now, I want to show my exe file in context menu after right click only on the folder, I also want to take folder name as an argument, than user click the exe file which I want to show in a right click context menu.

In this image you can easily understand, what i want.

FAB TECH
  • 13
  • 6
  • @Сергей Кох : I will be very grateful if you review this question. Thanks – FAB TECH Nov 19 '22 at 08:36
  • Please remove the macos tag. It is not related – Wolfgang Wilke Nov 19 '22 at 08:42
  • @popeinvestor I just removed the macos tag. Thanks – FAB TECH Nov 19 '22 at 09:01
  • 2
    With the windows context menu, you get the file/folder as an argument (same as a commandline argument). There's a ton of guides on how to add stuff to the context menu. [Google](https://www.google.com/search?q=windows+context+menu+add+entry)... – Edo Akse Nov 19 '22 at 10:34

1 Answers1

0

This is what I used for a simple app I wrote, it has stupid logic to check if it's run with or without arguments, and tries to add an entry to the context menu when it's run without arguments (eg. directly, not from context menu)

Just for simple explanation:

  1. Running the EXE directly will create a .reg file and run said registry file. This will add the menu entry to the context menu. It uses the path of the EXE to tell windows where the EXE can be found.
  2. After this, when right-clicking a file/directory/whatever, it will show the menu entry and if you click on that, it won't meet the contitions for the if statement, so it will continue with the rest of the script.

I'm not sure how this handles different Windows versions, as it was made specifically for Windows 11 and I added zero error checking.

import sys
import os


context_menu_text_string = "This is the text shown in the context menu"

if len(sys.argv) != 2:
    repl = sys.argv[0].replace("\\", "\\\\")
    with open("registryfile.reg", "w") as outfile:
        outfile.write(
            rf"""Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\Background\shell\{context_menu_text_string}\command]
@="{repl} \"%1\""
"""
        )
    os.startfile("registryfile.reg")
    input("Press enter to continue, after finishing the registry import...")
    os.remove("registryfile.reg")
    sys.exit(0)


full_file_path = sys.argv[1]

# your logic here, full_file_path contains the path to 
# the file/directory which was right-clicked
Edo Akse
  • 4,051
  • 2
  • 10
  • 21
  • I just tried, but the exe file not showing in the context menu. – FAB TECH Nov 19 '22 at 12:55
  • did you run it once (by just executing the EXE itself)? You should get a Windows notification asking if you want to add something to the registry. That is the part that adds it to the context menu. – Edo Akse Nov 19 '22 at 16:40
  • Now successfully showing my exe file in right-click context menu. But not showing in folder right-click menu – FAB TECH Nov 21 '22 at 06:52
  • see updated answer. You'll need to edit your registry to remove the previous registry key so you can remove it from the context menu... – Edo Akse Nov 21 '22 at 08:53
  • [See this answer](https://stackoverflow.com/a/29769228/9267296) for general details on adding folder context menus to Windows – Edo Akse Nov 21 '22 at 08:55