Keynote - Making Charts - Show Legend

Hi,

I’ve written the following code to generate some charts in Keynote. I was wondering - is there any way to show the legend?

tell application "Keynote"
		activate
		
		set thisDocument to make new document with properties ¬
			{height:764, width:1024, document theme:theme "White"}
		
		tell thisDocument
			tell slide 1
				set the base slide to master slide "Title - Top" of thisDocument
				set the object text of the default title item to "Chart Title"
				add chart row names {"ROW A"} column names {"Dataset 1", "Dataset 2"} ¬
					data {{55, 6}} type pie_2d group by chart column
			end tell
			set the newSlide to make new slide
			tell slide 2
				set the object text of the default title item to "Chart Title"
				add chart row names {"ROW A"} column names {"Dataset 1", "Dataset 2"} ¬
					data {{22, 70}} type pie_2d group by chart column
			end tell
			
		end tell
	end tell
end if

1 Like

Here is a way to do the job.

I added code allowing it to work worldwide.

# define the index of the theme to use
set WhiteIndex to 2

tell application "Keynote"
	set path2app to (path to it) as text
	set theBundle to (path2app & "Contents:SharedSupport:Templates:White") as «class furl» # use as alias if must run before 10.11
	set localMasterSlide to localized string "Title - Top" from table "Localizable" in bundle theBundle
	activate
	-- get theme names
	set installedThemes to name of every theme
	--> {"Noir", "Blanc", "Dégradé", "Démonstration", "Type moderne", "Exposition", "Ébauche", "Reportage photo", "Classique", "Ardoise", "Papier crème", "Artisanal", "Improvisation", "Renaissance", "Portfolio photo", "Éditorial", "Kyoto", "Canevas au pinceau", "Imprimerie", "Maroc", "Art", "Industriel", "Portfolio moderne", "Harmonie", "Papier millimétré", "Projet", "Formel", "Livre en cuir", "Rétro", "Livre relié", "Reliure lin", "Tableau noir", "Parchemin", "Blue Stripes_1680x1050"}
	set localTheme to item WhiteIndex of installedThemes
	--make new document with properties {document theme:theme localTheme}
	set thisDocument to make new document with properties ¬
		{height:764, width:1024, document theme:theme localTheme}
	
	tell thisDocument
		tell slide 1
			set base slide to master slide localMasterSlide of thisDocument
			set object text of default title item to "Chart Title"
			add chart row names {"ROW A"} column names {"Dataset 1", "Dataset 2"} ¬
				data {{55, 6}} type pie_2d group by chart column
		end tell # slide 1
		my showLegend() # show it in slide 1
		set newSlide to make new slide
		tell newSlide -- was slide 2
			set object text of default title item to "Chart Title"
			add chart row names {"Rhododendron"} column names {"Dataset 1", "Dataset 2", "Dataset 3"} ¬
				data {{22, 70, 40}} type pie_2d group by chart column
		end tell # newSlide
		my showLegend() # show it in slide 2
	end tell # thisDocument
end tell # Keynote

on showLegend()
	tell application "System Events" to tell process "Keynote"
		set frontmost to true
		tell window 1
			--class of UI elements --> {radio group, UI element, button, button, static text, scroll area, scroll area, scroll area, button, button, button, menu button, toolbar, static text, static text}
			--position of scroll areas --> {{2440, 480}, {1415, 271}, {1273, 271}}
			tell scroll area 1
				--class of UI elements --> {static text, checkbox, checkbox, static text, pop up button, pop up button, group, button, static text, UI element, static text, UI element, static text, static text, pop up button, scroll bar}
				--title of checkboxes --> {"Titre", "Légende"}
				# urge it to display the Legend
				if value of checkbox 2 = 0 then click checkbox 2
			end tell #  scroll area 1
		end tell # window 1
	end tell # System Events .
end showLegend

As you asked off list for a script doing the same kind of job with Numbers, I post the resulting code too.

(*
It may be useful to read : https://iworkautomation.com/numbers/keynote-pie.html
2016/12/03
*)
set theNumbersGraphType to 8
set tableName to "table4chart"
set sheetName to "Charts"

tell application "Numbers"
	activate
	set verNum to version
	considering numeric strings
		if verNum < "3.1" then error "The script requires Numbers version 3.1 or higher"
	end considering
	
	make new document # disable this instruction if you work upon a document already open
	tell document 1
		tell sheet 1
			tell current application to delay 0.1
			# Create a table matching our needs. Mainly a table with no header.
			make table with properties {name:tableName, row count:2, column count:2, header row count:0, header column count:0, footer row count:0}
			tell table tableName
				its properties
				tell row 2
					set value of cell 1 to 78
					set value of cell 2 to 22
				end tell
				set thisRangeName to "A2:B2"
				set selection range to range thisRangeName
				tell current application to delay 0.1
			end tell # table 1 of sheet 1
			
			# Charts have no name so only their index allow us to target a given one
			# Calculating the index of the new one is useful if we work upon an existing document with available chart(s)
			set chartNum to (count charts) + 1
			
			my createTheChart(theNumbersGraphType)
			
			set nbLoops to 40
			repeat nbLoops times
				tell current application to delay 0.1
				if exists chart chartNum then exit repeat
			end repeat
			# AppleScript always use point as unit.
			tell chart chartNum to set width to width * 1.8
			
			my ruleTitleAndLegend()
			
			my cutTheChart()
		end tell # sheet 1
		
		if not (exists sheet sheetName) then make new sheet with properties {name:sheetName}
		
		set active sheet to sheet sheetName
		tell sheet sheetName # the active one
			set chartNum to (count charts) + 1 # so you will be able to target the new chart if needed.
		end tell
		
		my pasteTheChart()
		
		# Now you may work upon sheet sheetName
		tell sheet sheetName # the active one
			try
				delete tables
			end try
			tell me to delay 0.5 # useful to get the table removal reflected in allowed position
			# Just to flag an odd behavior striking at least upon the French version.
			# If the defined xLeft value is 30, the chart will be 30 points from the left window edge
			# and its xLeft coordinate will be reported as 0 in the inspector
			# If the defined xLeft value is < 30 the chart will be 50 points from the left window edge.
			set position of chart chartNum to {10, 20}
		end tell # active sheet
		
	end tell # document 1
end tell # Numbers

#=====

on createTheChart(theGraphType)
	tell application "System Events" to tell process "Numbers"
		set frontmost to true
		tell current application to delay 0.1
		# instruction useful to get infos, useless in real life
		-- name of menu items of menu 1 of menu bar item 5 of menu bar 1
		(*Feuille, missing value, Saut de colonne, missing value, Numéro de page, Nombre de pages, Date et heure, missing value, Tableau, Graphique, Zone de texte, Figure, Ligne, Surligner, Commenter, missing value, Rangs copiés, Colonnes copiées, missing value, Formule, Équation MathType., missing value, Choisir.*)
		# instruction useful to get infos, useless in real life
		--name of menu item 10 of menu 1 of menu bar item 5 of menu bar 1
		--> "Graphique"
		# instruction useful to get infos, useless in real life
		-- name of menu items of menu 1 of menu item 10 of menu 1 of menu bar item 5 of menu bar 1
		--> {"Colonnes 2D", "Colonnes 2D empilées", "Barres 2D", "Barres 2D empilées", "Linéaire 2D", "Couches 2D", "Couches 2D empilées", "Circulaire 2D", "Nuage de points 2D", "Bulles 2D", "Mixte 2D", "2 axes 2D", missing value, "Colonnes 3D", "Colonnes 3D empilées", "Barres 3D", "Barres 3D empilées", "Linéaire 3D", "Couches 3D", "Couches 3D empilées", "Circulaire 3D", missing value, "Colonne interactive", "Barre interactive", "Nuage de points interactif", "Bulle interactive"}
		# instruction useful to get infos, useless in real life
		-- name of menu item theGraphType of menu 1 of menu item 10 of menu 1 of menu bar item 5 of menu bar 1
		--> "Colonnes 3D"
		tell menu bar 1 to tell menu bar item 5 to tell menu 1 to tell menu item 10 to tell menu 1 to click menu item theGraphType
	end tell # application "System Events" to tell process "Numbers"
end createTheChart

#=====

on ruleTitleAndLegend()
	tell application "System Events" to tell process "Numbers"
		set frontmost to true
		# instruction useful to get infos, useless in real life
		--name of checkbox of scroll area 3 of window 1
		--> {"Titre", "Légende", "Données masquées"}
		try
			# Enable Title
			if value of checkbox 1 of scroll area 3 of window 1 is 0 then
				click checkbox 1 of scroll area 3 of window 1
			end if
		end try
		try
			# Disable Legend
			if value of checkbox 2 of scroll area 3 of window 1 is 1 then
				click checkbox 2 of scroll area 3 of window 1
			end if
		end try
	end tell # application "System Events" to tell process "Numbers"
end ruleTitleAndLegend

#=====

on cutTheChart()
	tell application "System Events" to tell process "Numbers"
		set frontmost to true
		keystroke "x" using {command down}
	end tell
end cutTheChart

#=====

on pasteTheChart()
	tell application "System Events" to tell process "Numbers"
		set frontmost to true
		keystroke "v" using {command down}
	end tell
end pasteTheChart

#=====

Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) vendredi 2 décembre 2016 20:49:18

1 Like