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).
-
1What platform are you working/deploying on? How far can you go changing settings there? – Stefan Schmiedl May 15 '14 at 19:38
-
That's also part of the problem, its supposed to be universal/cross platform. – Kyle May 15 '14 at 19:38
-
In theory I could make seperate scripts for different OS's – Kyle May 15 '14 at 19:39
-
1universal/cross-plattform makes it easy: no solution. – Stefan Schmiedl May 15 '14 at 19:39
-
That's unfortunate...what if I write separate programs? – Kyle May 15 '14 at 19:41
-
let me write an answer ... the comment box is too small :-) – Stefan Schmiedl May 15 '14 at 19:41
-
@Kyle, what is universal? Does it have to work on **VMS** for example? You need to be more specific. – Cristian Ciupitu May 15 '14 at 20:13
-
@Christian Ciupitu it means cross platform, so any OS. – Kyle May 15 '14 at 20:23
2 Answers
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.

- 1
- 1

- 205,541
- 37
- 345
- 415
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?

- 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