How to get selected range in numbers in AppleScript

587 views Asked by At

I have an AppleScript that I wrote for Numbers a few years ago that used to work. Since then, I've upgraded my OS (probably more than once), which I'm sure also upgraded Numbers and other aspects of the system. My current environment is:

  • MacOS 12.2.1 (Monterey)
  • Numbers 11.2

The script appears to be failing (i.e., not doing what I want) pretty early on in the process:

...
tell application "Numbers"
    set scriptStart to current date
    tell table 1 of active sheet of front document
        set selRng to selection range
        set cellCount to count of cells of selRng
        repeat with currentCell in cells of the selRng
            my processCell(currentCell)
        end repeat
    end tell
    set scriptEnd to current date
    beep
    display dialog "DONE: " & my formatTime()
end tell
...

Specifically, the line that says: set selRng to selection range does not [any longer] seem to work (and yes, I have a contiguous range of cells selected before running the script). It appears (from debugging) that selRng is a list of 0 elements, even though I know eight cells selected before running the script.

So - can someone tell me how I retrieve (for use) the information about a preselected range of cells in AppleScript [with current versions of OS/Numbers]?

Additional Notes

  • Through additional debugging, I've verified that it is processing the correct sheet
  • I tried changing to selection range to to the selection range, but it did not appear to make any difference.
  • Trying to follow other postings I've found, I added the following code: set selRangeName to name of selection range When debugging, it came back as "E133:L133" which indicates that there is a range; however, the selected cells are "D122:K122" so there is also a rather strange disconnect
  • UPDATE: If I turn off "Categories" in the Numbers sheet, the name of the selected range is correct ("D122:K122"). However, the iteration through the cells still seems to fail because selRng is still a "list of 0 items". I still feel as if I'm making progress though.
  • UPDATE: On a whim (while reading some documentation) I changed set selRng to selection range to set selRng to cells of selection range and now selRng contains eight items!! However the iteration process still isn't working, but I think I'm getting closer...
1

There are 1 answers

0
Adam Stoller On

Since the original purpose for this posting was to get the selected range of cells, and since I was able to achieve that by working things out myself, I'm going to consider this question "answered" with the following code snippet:

 ...
  tell application "Numbers"
    set activeSheet to active sheet of front document
    set scriptStart to current date
    tell table 1 of activeSheet
        set selRng to cells of selection range
        set cellCount to count of selRng
        
        repeat with currentCell in cells of the selRng
            my processCell(currentCell)
        end repeat
    end tell
    set scriptEnd to current date
    beep
    display dialog "DONE: " & my formatTime()
  end tell
 ...

I will be posting a new question soon, probably, to figure out how to properly iterate through the list of cells in the selected range since that repeat loop above still doesn't seem to be working like it used to.