0

I have a project where the goal is to have a user double click on an xml file, which is sent to my python script to work with. What is the best way to do accomplish something like this? Can this even be done programmatically or is this some kind of system preference I have to modify? How can my program retrieve the file that was double clicked on (at least the name).

Kyle
  • 2,339
  • 10
  • 33
  • 67

2 Answers2

2

On Linux, change the XDG file associations. You'll need to first associate the extension with a MIME type, then register a .desktop file to open that MIME type.

On OS X, you have to put the Python program in an application bundle, and then add the file extension to the list of extensions which that program handles. This is done by editing the bundle's Info.plist.

On Windows, you have to change registry settings. Here is how to do it in WiX, if you use WiX to write your installer.

Summary: Probably best to just use an "open file" dialog within your application, or use a command-line interface, or something simpler.

Community
  • 1
  • 1
Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
0

This problem is a bit underspecified.

Say, you're on some Windows: You could assign the correct python invocation as default open action to the .xml file type, but this would then apply to all .xml files. And you might need elevated access rights to perform the change.

On Linux, you'd have to deal with various ways of desktop environments ... and then I'd open an issue because it does not work with xfe running under awesome on my box here.

In short: Solve this problem by avoiding it.

Can you make do with "drag this file onto the application icon"? Can you make the .xml files appear in a dedicated place and have a background task check for new ones?

Stefan Schmiedl
  • 505
  • 5
  • 21
  • Drag the icon on to the application may work. How would I go about doing that? – Kyle May 15 '14 at 19:49
  • Again, it depends on the OS. On Windows, it usually does the "right" thing, i.e. it would basically amount to "myprogram dragged_file". You'd have to provide a suitable shortcut loading your python script as "myprogram" – Stefan Schmiedl May 15 '14 at 19:52
  • Ok but then how would I get a python script to notice what file was dropped on to it? Or does the python script have to be in an application package for that to work? – Kyle May 15 '14 at 19:53
  • you drag "somefile.xml" onto a shortcut running "python.exe script.py" and windows executes "python.exe script.py somefile.xml" – Stefan Schmiedl May 15 '14 at 19:55
  • Ok right I get that, but programatically, inside my python script, how would I retrieve what file was dropped? – Kyle May 15 '14 at 19:56