How can I make a Python script to be a specific file type's (e.g., *.foo) default application? As in, when I double click the file in the Finder / Explorer I want the file to open in the Python script.
Is this possible to do in Win and/or OS X? The application is a PySide app if that matters.
On Windows:
.docx
file for this example)Open with...
python
Always use the selected program to open this kind of file
.Note: this will run the contents of the
.docx
file in context of the python shell. It will immediately close once it is finished evaluating the contents of the file. If you'd like to edit the file in a word processor, perhaps you should download notepad++, and select that application as the default.I found this old question while looking for an answer myself, and I thought I would share my solution. I used a simple c program to direct the arguments to a python script, allowing the python script to stay a script instead of needing to compile it to make things work. Here is my c program:
I then compiled the c program and set that as the default application for the file extension.
Then, in the python script YOUR_PYTHON_SCRIPT_HERE.py, I receive the argument like this:
theFile will contain the location of the file that is being opened
Get the contents of the file by using:
By example, here's a universal solution I wrote for: 1) opening a Windows desktop link (*.URL) that's been copied to a Linux box. Or 2) opening a Linux .Desktop link that's been copied to a Windows box.
Here's the Python script that handles both cases:
On Windows, use Scott H's method (via a bat file) to handle the association.
On Linux, right-click a Windows URL file. Choose Properties, and Open With. Click Add to add a new application. Then at the bottom of the "Add Application" window, click "Use a custom command". Then browse to the UseDesktopLink.py file and click Open. But before you click Add, in the textbox below "Use a custom command", put "python " before the filename (without the quotes). Then click Add and Close.
Hope that helps.
foo
Get Info
or Click on the file icon,then click Get info or click on the file and hit Command+IOpen With
pane that shows up, select the path to the python binarychange All
buttoncontinue
Mac OS X
On Mac OS X you can use Automator to create an application that calls your python app and passes the input file path as a string argument. In the application workflow wizard, add action "Run Shell Script", select
Pass input:
asas arguments
, and in the text box add:The
"$@"
passes along whatever arguments were in the input (aka the selected file) as strings. As long as your script is set up to deal with the input (sys.argv
) as a list of strings (the first one being the python app path), then it will work.When you save that Automator workflow, it is treated by OS X like any other app, and you can set that app as the default for files of type "*.foo". To associate "*.foo" with that app, right click a .foo file,
Get Info
,Open with: Other...
, choose the app you created in Automator, then click theChange All...
button.Windows
A similar but hopefully less-involved approach might work in Windows. You could probably create a batch file (
.bat
) with the following:The
%*
expands to all arguments.As long as you can associate a file extension with that batch file, then you could do that, and that's your solution. However, I haven't tried this Windows solution, so take it with a grain of salt. The Mac solution, on the other hand, I have tested.