views:

163

answers:

1

How do you set the selection for a table in Numbers using py-appscript?

This seems like it should be really simple to do but the solution is frustratingly evasive. I can get the current selection:

 current_table.selection_range

and I can get its cells:

  current_table.selection_range.cells()

but trying to set() either of them gets an angry appscript error.

+2  A: 

Looks like something like this works:

>>> current_table.selection_range.set(to=current_table.ranges[u'B3:C10'])

Note, looking at Number's script dictionary in AppleScript Editor or with ASDictionary, the property selection_range is defined as class range. So that's a clue that you need to come up with a reference of type range to set it.

Ned Deily
Thanks again Ned. You are really saving my ass today :) I was afraid I might not find anyone on SO who knew about `appscript` since it is such a specialized technology. The whole reference / element system takes some getting used to..
Chris Redford
And thanks for the extra advice too. I was going to ask you where you get your information and I am presuming from this note that you just use the `ASDictionary`'s and experience?
Chris Redford
You're welcome and thanks for the points. Yes, it does take some getting used to and, unfortunately, not all scriptable apps are as well-behaved as Numbers seems to be. Fortunately, the father of appscript, has, is a wonderful resource and he is known to hang out on SO as well as on the Python Mac SIG mailing list: http://dir.gmane.org/gmane.comp.python.apple
Ned Deily
A combination of ASDictionary, the standard AppleScript Editor.app, trying snippets in a Python interpreter, the py-appscript docs, web searches, and a bit of experience. The biggest problem, IMO, with scriptable apps is lack of consistency: there's no way to know for sure how a particular app's object model really works without exploring it and sometimes the waters are really murky.
Ned Deily