Make Python Scripts Droppable in Windows

I’m writing an FTP uploader in python, and I want to be able to drop a folder on my script to make it go, using the dropped folder as the argument. Unfortunately, Windows doesn’t recognize .py files as drop targets†.

A few minutes searching on google just turned up posts about other people with the same problem, and suggestions about how to make batch files that catch the arguments for you or turning your script into an EXE. No help there. So, I started digging around in the registry.

As it turns out, it’s really easy to turn python scripts into drop targets. All of the droppable file types (.exe, .bat, .cmd, etc.) have a shellex\DropHandler key in the registry. This key identifies the class id of a subroutine in Shell32.dll that turns each file into a command line argument for the script. All you have to do is add the same information to the Python file type key, and your file becomes a drop target. Poof.

And, it gets even better. If you look at .wsh (Windows Script Host) or .vbs (Visual Basic Script) files, you’ll find that the drop handler is different than the one for .bat, .cmd, or .exe. They both do the same thing, except that the wsh handler sends the arguments with long file names, whereas the exe handler sends them with short (8.3) file names.

Long File Names (WSH DropHandler): {60254CA5-953B-11CF-8C96-00AA00B8708C}
Short File Names (EXE DropHandler): {86C86720-42A0-1069-A2E8-08002B30309D}

I prefer long file names, so I decided to use the wsh handler for my python script.

Here’s a registry import file that you can use to do this. Copy the following into a .reg file and run it (Make sure that your .py extensions are mapped to Python.File).

Windows Registry Editor Version 5.00

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

Here’s a quick python script you can use to test this out:

import sys

for arg in sys.argv: 
    print arg

# wait for enter, otherwise we'll just close on exit
raw_input()

† I’m using the python.org installation of Python 2.5, it’s possible that the ActiveState installation does register python scripts as drop targets.

, , ,

  1. #1 by jim on August 17, 2008 - 5:21 am

    Very nice, thanks. :))
    You can also add an entry for each of Python.NoConFile and Python.CompiledFile
    then you’ll be able to drop on .pyw and .pyc files as well.

    credit to:
    http://bugs.python.org/issue1656675
    http://objectmix.com/python/390531-can-t-drag-drop-onto-py-windows-xp.html

    Windows Registry Editor Version 5.00

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

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

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

  2. #2 by Mark N. on October 23, 2009 - 1:22 pm

    I think this requires that a python script be able to take arguements at the command line like this:

    C:\my_documents>rename.py apple.txt

    if i try to run this I get:
    Traceback (most recent call last):
    File “K:\my_documents\python_stuff\resize\resize180.py”, line 11, in
    filename = sys.argv[1]
    IndexError: list index out of range

    all the examples of using parameters however require that you type:
    C:\my_documents>python rename.py apple.py

    I know this worked because it worked for me before but I’m not sure why there is a problem now.

  3. #3 by sara rosario on December 13, 2020 - 3:55 pm

    doesn’t work

    • #4 by Seth on December 13, 2020 - 11:30 pm

      It worked pretty well 12 years ago on Windows XP. But it doesn’t surprise me that four operating systems later, Microsoft may have changed something. Perhaps the shell32 class id has changed, or maybe drop handlers work differently now? Maybe it’s time to switch to Linux!

  1. ShareItNow!!! : Raymond Berg
  2. Drag and drop onto Python script in Windows Explorer – Python

Leave a comment