开发者

MacOSX open file with an application: where does it go to?

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 fi开发者_StackOverflow社区nder 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.

Py:

>>> print(sys.argv[1:])
'-psn_0_#######'

Where is the file reference?

Thanks in advance,


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.


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")
file.writelines(sys.argv[1:])

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


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

~/Library/Preferences/com.apple.LaunchServices.plist

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.


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


I described how to link certain filetypes to py2app-bundled Python applications at https://moosystems.com/articles/8-double-click-on-files-in-finder-to-open-them-in-your-python-and-tk-application.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜