How do you execute an OS X application (bundle) from Python?

has has.temp2 at virgin.net
Fri Nov 5 13:53:21 EST 2004


aleaxit at yahoo.com (Alex Martelli) wrote in message news:<1gmrrsj.7ljqd6f3sgkfN%aleaxit at yahoo.com>...

> > Using os.system to execute open is pretty simple, as other folks have
> > pointed out.
> 
> ...but doesn't do the P_WAIT: just like using open at the Terminal
> prompt, it immediately continues with your code even as TextEdit is
> starting up.  The idea of that snippet is to let the user edit a
> textfile for configuration, while until the user is done editing, then
> read the textfile etc etc.  And it works file on the Mac with vi too.

Ah right, I've got you now. I think you've got a problem in that
case... while the Mac OS is partly Unix that's not the same thing as
actually being Unix (or Windows); what's good for one may not be for
the other. For example, the following seems to work:

os.system('/Applications/TextEdit.app/Contents/MacOS/TextEdit
/Users/has/ReadMe.txt')

However:

1. I've no idea if this sort of thing is formally supported by Apple.

2. It launches a new TextEdit process even if one already exists; a
very unMac-like thing to do. Multiple documents should open under the
same process, and even if the OS doesn't enforce it your users
certainly won't like you doing anything else.

3. It doesn't return until the process quits. Simply closing the text
document - which is when you want it to proceed - won't quit the
process (nor should it, since this is not how Mac applications
operate). This will annoy users even more.


Personally I don't have a huge problem with the Mac's indifference to
this stuff: Mac Is Not Unix after all, and has always had its own ways
of doing things. The upper levels in particular (i.e. where the GUI
apps live) are firmly in the event-driven camp. The bit that ticks me
is that Apple go to all the trouble of creating this big, clever
event-driven environment, but don't think to add a decent system for
third-parties to request and receive notification events when
something of interest occurs elsewhere.

What you really want it to tell TextEdit to open a document in a new
window AND ask that it send you a notification event when that window
is closed. Right now, the only way to do that would be via a
third-party utility that lets you attach scripts to the application's
GUI objects and listen for messages that the app sends to those, but
that's hardly the ideal solution.


Under these circumstances, the usual solution is to poll TextEdit via
its Apple event interface to detect when the document window is no
longer open. It's a bit tedious and rather unelegant, and only viable
for applications that have adequate Apple event interfaces, but in
this case it ought to do the job without any problem, e.g.:

#!/usr/local/bin/pythonw

from appscript import *
from Carbon.File import FSSpec
from time import sleep

te = app('TextEdit.app')
se = app('System Events.app')


path = '/Users/has/ReadMe.txt'

te.open(FSSpec(path))
while (se.application_processes.filter(its.name == 'TextEdit').count()
        and te.documents.filter(its.path == path).count()):
    sleep(1)


HTH



More information about the Python-list mailing list