Hiding a column in Numbers

Anyone know how to script hiding a column?

Adding a column works:


set acac to add column after column "R" -- acac: column "S" of table ....

But none of these attempts hides column “R”:


set width of column "R" to 0 --reduces width to fit contents
set hidden of column "R" to true --error NoCanDo
set visible of column "R" to false --error NoCanDo

The menu command “Hide Column” has no associated keystroke. I suppose a GUI scripting approach could be used, but I thought I would check here first. I don’t see any property or element of “column” that suggests “hidden” or “visible.”

As far as I know, there is no way to hide a column with plain Vanilla script.
We aren’t allowed to set a column width to less than 8 pixels.
In the dictionary, visible apply only to a window.

You may define a shortcut to every menu item thru the system preference pane Keyboard.
If you don’t wish to do that you will have to use the standard GUI Scripting code triggering a menu item.

tell application "Numbers" to tell document 1 to tell sheet 1 to tell table 1
	set selection range to range "R1:R2" # Defines the column to hide
end tell

--my select_menu("Numbers", 6, 24) # The long code
my selectMenu("Numbers", 6, 24) # the short code

#=====
(*
my selectMenu("Pages",5, 12)
==== Uses GUIscripting ====
*)
on selectMenu(theApp, mt, mi)
	tell application "System Events" to tell process theApp
		set frontmost to true
		tell menu bar 1 to tell menu bar item mt to ¬
			tell menu 1 to click menu item mi
	end tell
end selectMenu

#=====
(*
my selectSubMenu("Pages",6, 4, 26)
==== Uses GUIscripting ====
*)
on selectSubMenu(theApp, mt, mi, ms)
	tell application "System Events" to tell process theApp
		set frontmost to true
		tell menu bar 1 to tell menu bar item mt to ¬
			tell menu 1 to tell menu item mi to tell menu 1 to click menu item ms
	end tell
end selectSubMenu

#=====
(*
useful to get the indexs of the triggered item
my select_Menu("Numbers", 6, 24) (* Tableau  > Hide column *)
*)
on select_menu(theApp, mt, mi)
	tell application id "com.apple.systemevents" to tell process theApp to tell menu bar 1
		get name of menu bar items
		(*{
01 - "Apple", 
02 - "Numbers", 
03 - "Fichier", 
		04 - "Édition", 
		05 - "Insertion", 
		06 - "Tableau", 
		07 - "Format", 
		08 - "Disposition", 
		09 - "Présentation", 
		10 - "Partager", 
		11 - "Fenêtre", 
		12 - "Aide"}
*)
		get name of menu bar item mt
		-- {"Tableau"}
		tell menu bar item mt to tell menu 1
			get name of menu items
			(* {
			01 - "Insérer un rang au-dessus", 
			02 - "Insérer un rang en dessous", 
			03 - missing value, 
			04 - "Insérer une colonne avant", 
			05 - "Insérer une colonne après", 
			06 - missing value, 
			07 - "Supprimer le rang", 
			08 - "Supprimer la colonne", 
			09 - missing value, 
			10 - "Rangs d'en-tête", 
			11 - "Bloquer les rangs d'en-tête", 
			12 - "Colonnes d'en-tête", 
			13 - "Bloquer les colonnes d'en-tête", 
			14 - "Rangs de bas de tableau", 
			15 - missing value, 
			16 - "Ajuster le rang au contenu", 
			17 - "Ajuster la colonne au contenu", 
			18 - missing value, 
			19 - "Répartir les rangs uniformément", 
			20 - "Répartir les colonnes uniformément", 
			21 - missing value, 
			22 - "Masquer le rang", 
			23 - "Afficher tous les rangs", 
			24 - "Masquer la colonne", 
			25 - "Afficher toutes les colonnes", 
			missing value, "Options Trier et filtrer", "Appliquer les règles de tri", missing value, "Remplir automatiquement les cellules", missing value, "Fusionner les cellules", "Ne plus fusionner les cellules", missing value, "Transposer les rangs et les colonnes", missing value, "Ajuster le texte"}
*)
			get name of menu item mi
			--{"Répartir les colonnes uniformément"}
			click menu item mi
		end tell -- menu bar.
		
	end tell -- System Events
end select_menu

#=====

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) jeudi 1 juin 2017 10:50:46

Seems to work, Yvan! I was able to resurrect Bill Cheeseman’s UI Browser to double-check. Now I will probably just incorporate this into the main code since it is only called once.


try
		set nextCell to first cell of currentColumn whose value is missing value
	on error --all cells in the column are filled
		set newColumn to add column after currentColumn
		set selection range of theTable to currentColumn
		hideColumn of me given theColumn:currentColumn
		logToConsole of me given theString:"New Column Added."
		set currentColumn to newColumn
		set nextCell to first cell of currentColumn
	end try



to hideColumn given theColumn:aColumn
	tell application "System Events"
		set GUIScriptingEnabled to UI elements enabled
		if GUIScriptingEnabled then
			tell application process "Numbers"
				click menu item 24 of menu 1 of menu bar item 6 of menu bar 1
			end tell
		end if
	end tell
end hideColumn

Here I combined the two components.
The hideColumn handler tried to do what is doing my proposal.
Alas, as an important instruction was missing “set frontmost to true”, it did nothing !

It’s not a Bill’s error, it’s just that the instruction was not required when he wrote its code.

tell application "Numbers" to tell document 1 to tell active sheet
	set theTable to table 1
	tell theTable
		set currentColumn to column 7 -- column "G"
		try
			set nextCell to first cell of currentColumn whose value is missing value
		on error --all cells in the column are filled
			set newColumn to add column after currentColumn
			set selection range to currentColumn # EDITED
			my hideColumn() -- of me given theColumn:currentColumn
			--logToConsole of me given theString:"New Column Added."
			set currentColumn to newColumn
			set nextCell to first cell of currentColumn
		end try
	end tell
end tell

on hideColumn() # passing a parameter to the handler is useless. It apply to the selected column.
	tell application "System Events"
		if (UI elements enabled) then
			tell process "Numbers"
				set frontmost to true # REQUIRED
				click menu item 24 of menu 1 of menu bar item 6 of menu bar 1
			end tell
		else
			display dialog "You must enable GUI Scripting thru the System Preference UniversalAccessPref, Accessibility pane."
		end if
	end tell
end hideColumn

I disabled the logToConsole instruction because I don’t know the code of the triggered handler.

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) vendredi 2 juin 2017 18:03:49