[Pythonmac-SIG] Converting to AppleEvents

has hengist.podd at virgin.net
Wed Dec 7 12:57:47 CET 2005

Brian Ray wrote:

>I have an AppleScript I am calling with os.system():
>	with timeout of 3600 seconds
>		tell application "MyApp"
>			reload startup
>		end tell
>	end timeout
>I am interested in various methods to convert this to AppleEvents.  
>Any sample codes of something similar?  There seems to be more than 
>one approach.
>I am trying to learn how to handle all my communication between Mach-
>O  applications and Python2.3 with AppleEvents, although I do not 
>know where to get started. Are there any tutorials on this topic? I 
>do not want to use any helper modules because I also must stick with 
>just Python2.3.

Any particular reason for that? e.g. If it's distribution worries, use py2app to build an application bundle with all dependencies included.

Standard options:

1. Execute AppleScript code via os.system() and osascript:

	osascript -e 'with timeout of 3600 seconds
		tell application "TextEdit"
			get name
		end tell
	end timeout'

Slow, inefficient, and lowest-common-denominator approach, but if speed isn't an issue and you don't need to do anything complex then it'll do the job.

2. Use the low-level Carbon.AE extension, which provides a minimal wrapper around the Apple Event Manager API. See Apple's site for AEM documentation. Expect to write lots and lots of code this way; not much fun at all.

3. Attempt to use Python's aetools/aetypes modules (I think you can forget about gensuitemodule: I've tried it under 10.4 with no success and assume it is now completely senile):

#!/usr/bin/env pythonw

from aetools import *
from aetypes import *

te = TalkTo(signature='ttxt', start=1)

print te.send('core', 'getd', {'----':ObjectSpecifier('prop', 'prop', 'pnam')})[1]

Not much fun either. Documentation is minimal, and figuring out how it works will require background knowledge of Apple events and the Apple Event Manager. You'll probably need to spend time troubleshooting bugs and omissions as well (these modules predate OS X and haven't been maintained in years) and the code is less than pleasant.

I really would recommend against trying #2 or #3 as they will only make you miserable. Use #1 for basic tasks, but for anything complex you should consider biting the bullet and using appscript or aem (see my site). The latest version of appscript includes an introduction to application scripting and a tutorial, and it's miles ahead of all of the above. Even if you do have to rig up your own solution in the end, you'll learn a lot more by taking aem apart and studying it than trying to figure this stuff out any other way.



More information about the Pythonmac-SIG mailing list