views:

154

answers:

2

I am using Appscript - a Python interface to AppleScript - in a project of mine that basically gets data from a Mac application.

Here is a sample code:

    asobj = app('Things').to_dos()[0]
    self.id = asobj.id()
    self.name = asobj.name()
    self.status = asobj.status()

Every invocation of the properties (id, name, status) does inter-process call and hence it is slow .. especially when you do the same for thousands of the objects.

Is there a way to get multiple properties at the same time via AppleScript's Python interface (appscript)?

+3  A: 

I'm not 100% sure how this would be expressed in Python, but most Applescript objects support a "properties" property which will return a dictionary containing key/value pairs for each of the supported properties of that object. I'm guessing that calling asobj.properties() would return an appropriate data structure from which you can then retrieve any individual properties you want.

Brian Webster
Yes, many scriptable applications do that and, with Appscript, return a Python dictionary:>>> app("TextEdit.app").properties(){k.class_: k.application, k.version: u'1.6', k.frontmost: False, k.name: u'TextEdit'}
Ned Deily
Thanks! Doing a `dir(asobj)` did not help before.
Sridhar Ratnakumar
A dir() on an Appscript reference object doesn't tell you anything about the Applescript object in the application. Nothing happens until you call a method (say,.get() or implicitly via ()) which causes Appscript to send Apple Events to the application object and construct a Python object based on the returned information.
Ned Deily
A: 

If you have a large number of elements, it will be quicker to grab your properties like this:

ref = app('Things').to_dos
ids = ref.id()
names = ref.name()
statuses = ref.status()

and then use Python's zip() function to rearrange them as needed. The appscript documentation has a chapter on optimisation techniques that explains this in more detail.

You should also grab copies of the ASDictionary and ASTranslate tools from the appscript website if you've not already done so. ASTranslate will help you convert application commands from AppleScript to appscript syntax. ASDictionary will export application dictionaries in appscript-style format and also enables appscript's built-in help() method, allowing you to explore application dictionaries interactively (much more powerful than dir()).

has
Individually getting properties is pretty slow.
Sridhar Ratnakumar
Actually, the above should be much faster than asking for each element's 'properties' property one at a time, as it only sends three Apple events in total: one to get all of the id properties, one to get all of the name properties, and one to get all of the status properties.Or you could even ask for app('Things').to_dos.properties(), which takes just one Apple event. That may or may not be faster than sending the three events to get the properties you want, depending on how long it takes Things and appscript to pack and unpack all the unwanted properties as well.
has