[Pythonmac-SIG] AppleScript events in Python

Dan Green dgreen@rewindtech.com
Tue, 29 Feb 2000 22:48:24 -0500


a couple dozen of you have probably already done this, but I thought I'd post
it just the same. It's a simple function that'll parse AppleScript apple events
sent to your application and pass them off to yet another handler with politely
formatted arguments.

add this to your __init__
	self.installaehandler('ascr', 'psbr', self.asEvent)

add this somewhere in your class
	def asEvent(self, _object=None, **kwargs):
		if not self._ashandlers.has_key(kwargs['snam']):
			return
		method = self._ashandlers[kwargs['snam']]
		attr = {}
		usrf = kwargs['usrf']
		# turn usrf into a dictionary
		for i in range(0, len(usrf), 2):
			attr[usrf[i]] = usrf[i + 1]
		apply(method, (), attr)

Then all you need to do is define the events to handle and their handlers.
Let's say you wanted the Retrospect backup software to notify you when the
server stopped running:

(in __init__)
	self._ashandlers = {}
	self._ashandlers['serverend'] = self.serverend

def serverend(self, **args):
	self.do_things()

Now if some smart mac person out there can figure out a way to send data back
to the originator of the event, we'd essentially have a way to write little
applescript helper apps in our beloved Python.

-- 
Dan Green           | pick yourself up off the side of the road with your
Developer           | elevator bones and your whip-flash tones / members only,
Rewind Technologies | hypnotizers move through the room like ambulance drivers