views:

69

answers:

3

I am writing a simple Python script with no GUI. I want to be able to drag and drop multiple files onto my python script and have access to their absolute paths inside of the script. How do I do this in Mac, Linux, and windows? For times sake, just Mac will be fine for now.

I've googled this question and only found one related one but it was too confusing. I am currently running Mac OS X Snow Leopard.

Any help is much appreciated.

Thanks!

+1  A: 

Usually when you drag a file onto a script/executable, the OS passes the path to that file as a command-line argument. Check sys.argv

Michael Mrozek
that's what I figured but when hovering over the script with a file, the script doesn't highlight. Like I can't actually drop a file onto it anyway. I did a chmod +x on it and have "#!/usr/bin/env python# encoding: utf-8" at the top if that makes a difference at all
A: 

This really is independent of python. It depends entirely on which file browser you're using and how it supports drag and drop.

Brendan Abel
So how would I support this with Mac OS's Finder file browser?
A: 

For OS X, the most straightforward way is to have your script run as part of an application bundle (.app). You can use something like py2app to build a python application. Another approach might be to use Automator or AppleScript to create an app that takes the input parameters and passes them to the python script. An example of Automator usage is here.

Ned Deily
My hopes was to make a single python script that would work on multiple systems where it just checks which system you were on and executed the appropriate code
As the song says, you can't always get what you want. On OS X, AFAIK, the whole drag-and-drop thing is tied into the app mechanism. Another approach which I deliberately did not mention is to have a helper app which will start and run a python script. The python.org distribution provides such a helper app but it presents a security risk and its use is discouraged (and it may be removed in a future python release). It's a risk because it can launch any python script even ones ownloaded by a web browser. Much better and safer to have a specific app to wrap your script.
Ned Deily
Ah alright. Too bad I can't make it generic. I will go ahead and give py2app a try. Thanks for the info!