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).

有帮助吗?

解决方案

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.

其他提示

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?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top