Take the 2-minute tour ×

I posted this basic question before, but didn't get an answer I could work with.

I've been writing applications on my Mac, and have been physically making them into .app bundles (i.e., making the directories and plist files by hand). But when I open a file in the application by right clicking on the file in finder and specifying my app, how do I then reference that file?

I mostly use python, but I'm looking for a way that is fairly universal.

My first guess was as an argument, as were the answers to my previous post, but that is not the case.


>>> print(sys.argv[1:])

Where is the file reference?

Thanks in advance,

share|improve this question
Did anyone figure out exactly how to do this? –  cwd Dec 16 '11 at 20:59
add comment

4 Answers

up vote 3 down vote accepted

The file is passed by the Apple Event, see this Apple document. You need to receive that from inside your Python script. If it's a PyObjC script, there should be a standard way to translate what's explained in that Apple document in Objective-C to Python.

If your script is not a GUI app, but if you just want to pass a file to a Python script by clicking it, the easiest way would be to use Automator. There's an action called "Run Shell Script", to which you can specify the interpreter and the code. You can choose whether you receive the file names via stdin or the arguments. Automator packages the script into the app for you.

share|improve this answer
Thanks. I will look into that. –  Sky Sep 8 '10 at 2:34
add comment

This is not an answer but it wouldn't fit in the comments. To respond to @Sacrilicious and to give everyone else insight on this:

@Sacrilicious You're talking about something different. Download this sample application, it's a python script wrapped as an "App". Look inside and find a 4-line python script: myscript.app/Contents/MacOS/myscript - which will print the arguments using

file = open("/tmp/test.txt", "w")

Stick it in your Applications folder. Then right click some file and choose "Open With" and select this myscript.app.

Now take a look at /tmp/text.txt and you'll see that something like -psn_0_####### is there and not the name of the file you had selected "open with". This is because the file is passed using Apple Events and not a filename as an argument.

So this question is asking how can you access the filename of the thing that was passed in the python script wrapped in an OS X .app application wrapper, and if someone can let me know that they'll get the Bounty :)

share|improve this answer
add comment

Are we referring to the file where per-user binding of file types/extensions are set to point to certain applications?


The framework is launchservices, which had received a good amount of scrutiny due to 'murkiness' early in 10.6, and (like all property list files) can be altered via the bridges to ObjectiveC made for Python and Ruby. Here's a link with Python code examples for how to associate a given file type with an app.

share|improve this answer
No, you have misunderstood the question. I've posted an "answer" with information / clarification. Take a look if you'd like more information on what this is about :) –  cwd Dec 17 '11 at 6:03
add comment

I've never heard of it being done without a Cocoa / Carbon wrapper.

share|improve this answer
add comment

Your Answer


