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('Thing开发者_运维知识库s').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)?
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.
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()).
精彩评论