AppleScript Numbers: Iterate through range of cells

1k 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) when trying to iterate through the range of pre-selected cells:

 ...
  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 know from debugging statements (not shown) in the loop that it never gets there and so repeat with currentCell in cells of the selRng is not the correct way to loop through the selRng (which I've also verified contains eight cells).

I'm going to continue trying out different things by searching the web for (good) examples, but if someone here can provide pointers it might save me a lot of time...

Like I said, this used to work...

UPDATE-1: As with my previous posting, I've seemed to solve the initial question, but then the problem just morphs into something else. The loop changed to:

repeat with currentCell in selRng
    my processCell(currentCell)
end repeat

OR:

repeat with currentCell in selRng
    set cellName to name of currentCell
    my processCell(cellName)
end repeat

In the first case, I end up with currentCell being set to:

item 1 of {cell "D122" of table 1 of sheet 2 of document id "66DF865E-D1EC-4E4A-8A0E-E66213CF0E76", cell "E122" of table 1 of sheet 2 of document...}

Which doesn't work because it isn't a cell, it still looks like a range.

In the second case, I end up with a string "D122" which doesn't work for the called routine that expects an actual cell to be passed to it.

So now I either have to figure out how to just get the actual first cell in the range (first coding) or how to convert the string to an actual cell or cell reference (second coding)...

UPDATE-2:

If I change the repeat loop to:

repeat with i from 1 to count of selRng
  set currentCell to cell i
  my processCell(currentCell)
end repeat

currentCell is set to A1 - not what I want.

If I change it to:

repeat with i from 1 to count of selRng
  set currentCell to cell i of selRng
  my processCell(currentCell)
end repeat

I get an error "Can’t get cell 1 of {cell "K122" of table 1 of sheet 2 of document id "66DF865E-D1EC-4E4A-8A0E-E66213CF0E76" of application "Numbers"}."

So the first case gives me the wrong cell, and the second case appears to leave me where I was before, trying to extract the i'th cell element of the range. (Arrgh :-))

1

There are 1 answers

0
Adam Stoller On

Well, I figured it out. The new code is:

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      
        repeat with i from 1 to count of selRng
            set currentCell to item i of selRng
            my processCell(currentCell)
        end repeat
    end tell
    set scriptEnd to current date
    beep
    display dialog "DONE: " & my formatTime()
end tell

Note the use of item I of selRng instead of cell I of selRng