pythonvisual-studio-codebatch-file

how to enable seperate action for double click and drag and drop for python files


I have a simple python script and I want to enable dragging and dropping files into the python script in Windows 11. This works. But only if I set python launcher as default application for python file types. I want to launch my editor when double clicking. My idea is to create a batch file as default application for the python script and launch my editor if no parameters are passed and launch it with python launcher if it has parameters. I cannot get it to fully work. But when opening the editor command window is created that doesn't close automatically.

test drag.py:

import sys
import os

if __name__ == "__main__":
    test_file_name = os.path.join(os.path.split(sys.argv[0])[0], "test.txt")
    if len(sys.argv) > 1:        
        with open(test_file_name, 'w', newline='\n') as f:
            f.write("file names:")
            f.write("\n") 

            for file_path in sys.argv[1:]:
                f.write(file_path)
                f.write("\n") 

    else:
        with open(test_file_name, 'w', newline='\n') as f:            
            f.write("no file names")
            f.write("\n") 

C:\drag\python_drag.cmd:

@echo off

if not "%~2"=="" (
    :: Arguments detected, so files were dragged on it. Calling it with python.
    START /B python.exe %*  
) else (
    :: No arguments detected, so double clicked. Opening it with editor (vscode)
    START /B code %1
)

Open command key is updated via command line:

reg add "HKEY_CLASSES_ROOT\Python.File\shell\open\command" /ve /t REG_EXPAND_SZ /d "\"C:\drag\python_drag.cmd\" \"%L\" %*" /f

Set drophandler if needed (if installing latest python version with py launcher it should be set). enable_python_drag_and_drop.reg:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Python.File\shellex\DropHandler]
@="{60254CA5-953B-11CF-8C96-00AA00B8708C}"

When starting python a window briefly opens (you can only see the outline) and then it closes automaticallt.

When starting VSCODE it opens an additional window that doesn't close. How to fix it?

Edit: I figured out how to at least minimize the window:

START /w /min cmd /c "call code %1"

or:

START /w /min cmd /c "call code.cmd %1"

Solution

  • code.exe has been moved out of the bin folder. Inside the bin folder is only code and code.cmd. It is now one folder up. Running code.exe directly solves the problem. No window is created. Here is C:\drag\python_drag.cmd:

    @echo off
    
    if not "%~2"=="" (
        rem Arguments detected, so files were dragged on it. Calling it with python.
        START "Python Script Execution" python.exe %*  
    ) else (
        rem No arguments detected, so double clicked. Opening it with editor (vscode)   
        START "" "%LOCALAPPDATA%\Programs\Microsoft VS Code\code.exe" %1
    )
    

    And it's added with this command:

    reg add "HKEY_CLASSES_ROOT\Python.File\shell\open\command" /ve /t REG_SZ /d "\"C:\drag\python_drag.cmd\" \"%L\" %*" /f