pyinstaller/pyinstaller — pull request #50 by dhyams (2013-04-10 20:18:52+00:00)
Title: Add argv_emulation for OSX

What's New
================
This pull request adds a feature that is practically the same as the argv_emulation feature in py2app.

Background
=================
If a user double clicks a document that is associated with an application on OSX (for example, a .doc file), OSX tells the associated application about the file via AppleEvents. This is distinctly different than other OS'es, where the filename is placed into main's argv list of strings, and can be parsed from there.

To make matters worse, PyInstaller spawns a child process as it is bootstrapping the executable, and your Python code runs in this child process. By experimentation, it was found that the AppleEvents never make it to the child process, which is not that surprising. So, we must, in PyInstaller, look for and "cache" the AppleEvent results if we are to try to look for them.

Solution
===================================================
The way this is solved in this pull request is to run a small event loop in the parent
process (before the forking), and look specifically for OpenDocument AppleEvents (other types of AppleEvents can be added later, if necessary). If an OpenDocument event is
spotted, the filename is extracted from it, and placed in argv. Therefore, the Python app needs only to query sys.argv to see any filenames that were passed to the application via this AppleEvent mechanism (just like other OS'es).

Note that AppleEvents are used in two circumstances: 1) the user double clicks a document associated with your app (which must be done via an Info.plist), or 2) the user drags and drops a document into your app's icon. Both situations have been tested and are working in this pull request.

Note that this pull request only addresses the "startup" AppleEvents as described above. If the user expects more AppleEvents after their Python code is started, obviously it is their responsibility to set up the appropriate handlers.

Example
=======================================================

A PyInstaller-bundled app is in /Applications/GreatApp.app, and has an appropriate Info.plist such that it is associated with files that have a foo extension. If then the
user double clicks the file "a.foo" that is in /home/user, the resulting sys.argv on the
Python side will look like:

['/Applications/GreatApp.app/Contents/MacOS/GreatApp','/home/user/a.foo']

Potential Problem Areas
==========================
The OSX bootloaders now link against the Carbon framework. I don't think this is a problem in general, but I feel the need to point it out.

This pull request cleans the (I think useless) -psn_xxxx command line option that is always passed when an OSX app is double clicked. It can be added back in easily (see bootloader/common/main.c), but I thought that I would mention the change in behavior.

This pull request has been tested with the 32-bit Darwin bootloaders; I wasn't sure offhand how to compile and test the 64-bit ones.