Sort Specific Column in Apple Numbers

@gglick scripted selecting a column here:

How can this be extended to then sort the selected column?

The sort command seems to only exist within a dropdown menu
sort menu

I tried editing @gglick's script by looking into Number's dictionary but it seems the sort commands are for rows and not columns.

I then tried @DanThomas UI Browser to GUI script but again was stopped by my lack of scripting skills.

1 Like
--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2021/07/18 13:02
# dMod: 2021/07/18 13:02 
# Appl: Numbers
# Task: Sort Table 1 of Active Sheet by Column Number.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Numbers, @Sort, @Column, @Table
--------------------------------------------------------

tell application "Numbers"
   tell front document
      tell active sheet
         tell table 1
            sort by column 2 direction ascending
         end tell
      end tell
   end tell
end tell

--------------------------------------------------------
1 Like

Thank you. It looks so easy afterwards.

2 Likes

Sort by selected column

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!

2 Likes

Perfect, simple, and helpful; thank you, @Nige_S!

One more if you're up for it.

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.

Any way to Apple or another script that?

1 Like

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
2 Likes

You're right. Best stick with the contextual menu in this instance.

This further explanation and seeing how to incorporate a list into this example is very useful.

Thank you!