pythoniworksourceforge-appscriptpy-appscript

How to access the current table in Numbers?


How do I access the current table in Numbers using py-appscript?


For posterity, the program I created using this information clears all the cells of the current table and returns the selection to cell A1. I turned it into a Service using a python Run Shell Script in Automator and attached it to Numbers.

 from appscript import *

 Numbers = app('Numbers')
 current_table = None
 for sheet in Numbers.documents.first.sheets():
      for table in sheet.tables():
           if table.selection_range():
                current_table = table

 if current_table:
      for cell in current_table.cells():
           cell.value.set('')

      current_table.selection_range.set(to=current_table.ranges[u'A1'])

It was used to clear large tables of numbers that I used for temporary calculations.


Solution

  • >>> d = app('Numbers').documents.first()  # reference to current top document
    

    EDIT: There doesn't seem to be a straight-forward single reference to the current table but it looks like you can find it by searching the current first document's sheets for a table with a non-null selection_range, so something like this:

    >>> nu = app('Numbers')
    >>> for sheet in nu.documents.first.sheets():
    ...   for table in sheet.tables():
    ...     if table.selection_range():
    ...        print table.name()