Another step forward, I looked in the Numbers dictionary before asking this!
I see columns, like cells, are defined by a range. I found that, surprisingly, a single cell is considered a range. Not completely intuitive, but I can work with it.
So if a single cell can be a range, I assume a single column can be a range as well.
Here's the 'buried lead' can the selected column be the object of the sort instead of predefining a particular column?
I have the following AS that lets me set the background color of a selected cell and tried to adapt it to sorting a selected column but returned errors.
-- insert desired hex colors associated with each code value in a cell between the (" ") -
-- click in table first, then run
tell application "Numbers"
tell front document's active sheet
tell (first table whose selection range's class is range)
set selRange to selection range
repeat with c in selRange's cells
set c's background color to my hexColortoRGB("#FFFFFF")
end repeat
end tell
end tell
end tell
-- handlers
property hexChrs : "0123456789abcdef"
on hexColortoRGB(h) -- convert
set h to text 2 thru -1 of h -- remove leading #
if (count h) < 6 then my badColor(h)
set rgbColor to {}
try
repeat with i from 1 to 6 by 2
set end of rgbColor to ((my getHexVal(text i of h)) * 16 + (my getHexVal(text (i + 1) of h))) * 257
end repeat
end try
if (count rgbColor) < 3 then my badColor(h)
return rgbColor
end hexColortoRGB
on getHexVal(c)
if c is not in hexChrs then error
set text item delimiters to c
return (count text item 1 of hexChrs)
end getHexVal
on badColor(n)
display alert n & " is not a valid hex color" buttons {"OK"} cancel button "OK"
end badColor
If you look at the AS dictionary range item you'll see it contains columns. So you could "sort by the column which is the first column of the selected range":
tell application "Numbers"
tell front document's active sheet
tell (first table whose selection range's class is range)
sort by (column 1 of selection range) direction ascending
end tell
end tell
end tell
That let's you select multiple columns and sort by the first, which is neat!
There is a contextual menu option to sort by selected rows. The contextual menu also registers the column the cursor is in when the contextual menu is evoked and sorts the selected rows by that column.
If you are selecting by right-click you may as well use the contextual menu. If the sort column is a constant, eg the third column in the selected range, change column 1 in the middle line of the script to suit, eg column 3.
If you want to allow the user to pick the column to sort by you'll need to throw a dialog. Something like this will do it, but you should add error handling for if the dialog is cancelled and so on:
tell application "Numbers"
tell front document's active sheet
tell (first table whose selection range's class is range)
set theList to name of (every column of selection range)
set sortCol to item 1 of (choose from list theList)
sort by column sortCol direction ascending
end tell
end tell
end tell