Script does not always run as predicted.

The following script runs fine when executed from AppleScript editor but skips the “System Events” bit when run from the Menu bar:


tell application "Numbers" to tell document "PV Generation" to tell sheet "Totals" to tell table "Yearly Totals"
	activate
	set selection range to cell range -- selects table
	tell application "System Events"
		tell process "Numbers"
			click menu item "Unhide All Columns" of menu "Table" of menu bar 1
			click menu item "Unhide All Rows" of menu "Table" of menu bar 1
			key code 53 -- ESC to deselect table
		end tell
	end tell
end tell

Model: iMac 24" Early 2009
AppleScript: 2.8.1
Browser: Safari 10
Operating System: Mac OS X (10.10)

What is surprising me is that you get it to work in the Script Editor because the syntax used is wrong.

You wrote :
click menu item “Unhide All Columns” of menu “Table” of menu bar 1
when it is supposed to be :
click menu item “Unhide All Columns” of menu 1 of menu bar item “Table” of menu bar 1

As I don’t use the system in English I edited your script so that it’s no longer localisation dependent and it works flawlessly.

--tell application "Numbers" to tell document "PV Generation" to tell sheet "Totals" to tell table "Yearly Totals"
tell application "Numbers" to tell document 1 to tell sheet 1 to tell table 1
	activate
	set selection range to cell range -- selects table
	tell application "System Events"
		tell process "Numbers"
			set frontmost to true
			--tell menu bar 1 to tell menu bar item 6 to tell menu 1 to get name of menu items
			(*{
			01 - "Insérer un rang au-dessus", 
			02 - "Insérer des rangs en dessous", 
			03 - missing value, 
			04 - "Insérer une colonne avant", 
			05 - "Insérer des colonnes 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 les rangs", 
			23 - "Afficher tous les rangs", 
			24 - "Masquer les colonnes", 
			25 - "Afficher toutes les colonnes", 
			26 - 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"}*)
			click menu item 25 of menu 1 of menu bar item 6 of menu bar 1
			click menu item 23 of menu 1 of menu bar item 6 of menu bar 1
			key code 53 -- ESC to deselect table
		end tell
	end tell
end tell

At first call you would perhaps be urged to authorize SystemUIServer.app to drive applications.
You will be asked to enter the “Security & Privacy” Preference Pane
Open the lock
Check the box in front of the entry “SystemUIServer.app”
Then you will be able to use the script

Remember that you may use FastScripts to trigger the scripts with shortcuts.
If I remember well, it’s free as long as you don’t need more than 10 shortcuts.

Yvan KOENIG running Sierra 10.12.0 in French (VALLAURIS, France) lundi 24 octobre 2016 15:52:01

Hi

Thank you for your reply.

All that was needed was to enable 'SystemUIServer" in 'Security & Privacy".

Thank you.