[Pythonmac-SIG] deconstruct a py-appscript reference

has hengist.podd at virgin.net
Sat Dec 12 19:57:08 CET 2009

Mark Sanvitale wrote:

> I am new to both python and to appscript (I took up both at the same time).  I am curious about what I think is an internal detail of the appscript module and its classes.  Given my newbie status, I apologize in advance if my terminology for appscript things strays from the standard.
> I have a reference returned by calling get() on some other reference (e.g. asking an iphoto album for its child list).  The reference I get back is fully resolved to a unique item (i.e. of the form app(u'/Applications/iPhoto.app').albums.ID(4294967326) and not involving a by-name lookup or query).  Now, I can use the returned reference to get() its properties, however, if the property I am after is "id" then it would be wasteful (i.e. extra IPC) to perform a get() for the id property because I can see it is a component of the reference I already have.
> Thus, my question is how can I get the id directly out of such a reference?  Presumably, via some means more elegant than parsing a string version of the reference.  (So far, I have gotten as far as poking around myRef.AS_appdata and myRef.AS_aemreference but have not yet found the magic word.)

If deep appscript juju is your thing, the aem package's Query subclasses provide a visitor method, AEM_resolve, for walking the entire reference. (The appscript.referencerenderer module uses it.) 

However, unless you're sending many thousands of events then asking the application for the object's id is unlikely to be a performance bottleneck. Personally I'd just ask the application, and maybe run a Python profiler if I think I'm still not getting my money's worth.

If you are sending many thousands of events, there are probably other optimisation techniques you should be using anyway. Despite superficial appearances, Apple event IPC is a combination of RPC+queries; trying to apply object-oriented idioms to appscript code when dealing with large data sets is a common cause of poor performance. See chapter 13 of the appscript manual for more information, and post back here if you need more advice.


Control AppleScriptable applications from Python, Ruby and ObjC:

More information about the Pythonmac-SIG mailing list