Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How to iterate over individual (non-adjacent) selected cells in Numbers using AppleScript?

There a lot of documentation and tutorials shows how to work with selected cell ranges.

However, I need to iterate over the values in individual cells selected with Cmd-Click.


User uploaded file

OS X Yosemite (10.10.4)

Posted on Jul 2, 2015 5:12 PM

Reply
7 replies

Jul 2, 2015 8:50 PM in response to nikkikom

Unfortunately when you make a non-contiguous selection like that the selection range becomes the entire smallest rectangular range that contains the cells you have selected.


User uploaded file



You can iterate left to right, row by row through the cells in that rectangular range like this:


tell application "Numbers"

tell document 1's active sheet

set t to first table whose selection range's class is range

repeat with c in t's selection range's cells


-- code goes here

end repeat

end tell

end tell



You could distinguish those cells via background color or other property accessible to AppleScript and have logic in your code that affects those cells only, something like this:


tell application "Numbers"

tell document 1's active sheet

set t to first table whose selection range's class is range

repeat with c in t's selection range's cells

if c's background color is "red" then


-- code goes here

end if

end repeat

end tell

end tell

.


With more details on what you are trying to accomplish, it may be possible to suggest a solution.


SG

Jul 3, 2015 1:57 AM in response to SGIII

I cannot see any difference between the properties of selected and unselected cells:



(selected) get properties of item 1 of every cell of selection range of table 1 of sheet 2 of documentid "44569CAA-DA28-4578-AA90-7693B243EA5F"


--> {vertical alignment:top, row:row "2" of table 1 of sheet 2 of documentid "44569CAA-DA28-4578-AA90-7693B243EA5F", class:cell, font name:"Helvetica", formatted value:missing value, background color:missing value, formula:missing value, name:"B2", text wrap:true, text color:{0, 0, 0}, alignment:auto align, column:column "B" of table 1 of sheet 2 of documentid "44569CAA-DA28-4578-AA90-7693B243EA5F", format:automatic, font size:10.0, value:missing value}

(unselected) get properties of item 2 of every cell of selection range of table 1 of sheet 2 of documentid "44569CAA-DA28-4578-AA90-7693B243EA5F"


--> {vertical alignment:top, row:row "2" of table 1 of sheet 2 of documentid "44569CAA-DA28-4578-AA90-7693B243EA5F", class:cell, font name:"Helvetica", formatted value:missing value, background color:missing value, formula:missing value, name:"C2", text wrap:true, text color:{0, 0, 0}, alignment:auto align, column:column "C" of table 1 of sheet 2 of documentid "44569CAA-DA28-4578-AA90-7693B243EA5F", format:automatic, font size:10.0, value:missing value}


What I want to accomplish in general is to let user to select individual cells from matrix and then make some calculations in applescript over the values in selected cells only. In particular I'd like to solve the "knapsack problem" just for the selected cells, then let the user select different set of items and so on. I.e. make this process as interactive as it is possible with Numbers and Script.

Jul 3, 2015 6:19 AM in response to nikkikom

nikkikom wrote:


What I want to accomplish in general is to let user to select individual cells from matrix and then make some calculations in applescript over the values in selected cells only. In particular I'd like to solve the "knapsack problem" just for the selected cells, then let the user select different set of items and so on. I.e. make this process as interactive as it is possible with Numbers and Script.


If instead of just selecting the cells and running the script, the user instead selected the cells and bolded them (with the various cells selected, typing command-b works to bold them all at once) and then ran the script, you could do something like this (change font to match what you are using in your set-up).


SG


tell application "Numbers"

tell document 1's active sheet's table 1

set selValues to value of cells whose font name is "Helvetica-bold"


-->{3.0, 11.0, 31.0, 33.0}

end tell

end tell


repeat with i in selValues's items


--do something here to solve knapsack problem

end repeat

Jul 3, 2015 9:14 AM in response to nikkikom

nikkikom wrote:


to copy selected cells with the script, to paste it into temporary table and traverse that table instead of original.


How do you copy selected cells with the script? Gui-scripting gets pretty awkward. Curious about your solution there?


Copying manually to the clipboard takes a command-c and then command-v. Same amount of work as command-b.


Is there an advantage to having the values in 2d matrix as you would with a pasted temporary table?


SG

Jul 3, 2015 12:03 PM in response to nikkikom

Here's another way of implementing SGIII's idea, assuming the default font is Helvetica :


tell application "Numbers"

activate

repeat until frontmost is true

end repeat

tell application "System Events" to keystroke "b" usingcommand down--> Helvetica-Bold

tell document 1's active sheet'stable 1

set theCells to name of cells of selection range whose font name is "Helvetica-Bold"

set theValues to value of cells of selection range whose font name is "Helvetica-Bold"

end tell

tell application "System Events" to keystroke "b" usingcommand down--> Helvetica

end tell


{theCells, theValues}

How to iterate over individual (non-adjacent) selected cells in Numbers using AppleScript?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.