[Pythonmac-SIG] [ann] PyOSA 0.1.0, Appscript Installer 1.5.1 released

has hengist.podd at virgin.net
Sat Mar 31 23:42:39 CEST 2007


Jacob Rus wrote:

> > For example, you could create a Mail rule that runs an 'AppleScript'
> > action whenever new messages are received.
>
> Could someone make such an example script (or something similar), and
> put it up somewhere?

There is a very simple example script included with PyOSA that prints  
the arguments for the 'perform_mail_action_with_messages' call to  
stdout, which you can view in Console.app.


> What happens when a specific handler is called on
> that script?  It presumably calls a python function of the same name.

Yes. Top-level functions act as event handlers. A 'run' event calls  
the 'run' function, an 'open' event calls the 'open' function, a  
'perform_mail_action_with_messages' event calls the  
'perform_mail_action_with_messages' function, etc.

(Events that don't have a human-readable name and user-defined  
subroutine calls are treated differently, however, so ask if you need  
to support those sorts of events.)


> Is there some mapping of applescript handlers with named parameters to
> python functions, etc.?

It's the same as the mapping used by appscript commands, except that  
calls are going in the opposite direction, of course. If an event  
provides a direct parameter, that will be assigned to the first (i.e.  
positional) parameter of your function. Any keyword parameters are  
then assigned to function parameters of the same names. e.g. Mail's  
'perform_mail_action_with_messages' event has the following  
dictionary definition:
	perform_mail_action_with_messages -- Script handler invoked by rules  
and menus that execute AppleScripts. The direct parameter of this  
handler is a list of messages being acted upon.

		list of message -- the message being acted upon
		[in_mailboxes mailbox] -- If the script is being executed by the  
user selecting an item in the scripts menu, this argument will  
specify the mailboxes that are currently selected. Otherwise it will  
not be specified.
		[for_rule rule] -- If the script is being executed by a rule  
action, this argument will be the rule being invoked. Otherwise it  
will not be specified.


The corresponding event handler would be:

	def perform_mail_action_with_messages(messages, in_mailboxes,  
for_rule):
		...

The positional parameter's name can be anything you like (I used  
'messages' here), just as long as doesn't match a keyword parameter  
name.


One more thing: it's okay for an event handler to leave out  
parameters that it doesn't need. So you could also write:

	def perform_mail_action_with_messages(messages):
		...

	def perform_mail_action_with_messages(in_mailboxes, for_rule):
		...

	def perform_mail_action_with_messages():
		...

and so on, depending on which parameters you're interested in.


HTH

has

p.s. If anyone comes up with better scripts they'd like to share,  
please let me know. Help with improving the documentation would also  
be very welcome.

-- 
http://appscript.sourceforge.net
http://rb-appscript.rubyforge.org
http://appscript.sourceforge.net/objc-appscript.html



More information about the Pythonmac-SIG mailing list