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

has has.temp2 at virgin.net
Thu Nov 4 18:13:48 EST 2004


dfh at forestfield.co.uk (David Hughes) wrote in message news:<70bb9f8d.0411040250.1f4ac1a at posting.google.com>...
> For example, in Python in a Nutshell, Alex Martelli shows how you can
> run a Windows (notepad.exe) or Unix-like (/bin/vim) text editor using
>       os.spawnv(os.P_WAIT, editor, [textfile])
> But how would you call the OS X text editor /Applications/TextEdit.app
> - which appears to be a whole directory inside /Applications?

Using os.system to execute open is pretty simple, as other folks have
pointed out. The other way is to use Apple events, the standard
high-level IPC system used by Mac GUI apps. The AE support currently
in the standard library leaves something to be desired and is due for
replacement. Much improved, though unfinished, AE support is available
from my site:

http://freespace.virgin.net/hamish.sanderson/appscript.html

Fastest way to open a document is via the lower-level aem package
(lower overheads, though you have to use raw AE codes):

from aem.send import Application
from Carbon.File import FSSpec
Application('/Applications/TextEdit.app').event('aevt', 'odoc',
{'----':FSSpec(pathToFile)}).send()


Alternatively, the high-level appscript package wraps all this stuff
in OO-like syntactic sugar and human-readable terminology (takes
longer to initialise as it has to retrieve and parse the application
terminology):

from appscript import *
app('TextEdit.app').open(FSSpec(pathToFile))


HTH



More information about the Python-list mailing list