Create charts with AppleScript

I am trying to create a simple bar chart in Pages/Numbers using AppleScript but cannot find how to do this. Found some code in my ‘Learn AppleScript’ manual but it fails to compile with the message “Syntax Error Expected end of line but found class name” highlighting the word ‘chart’. Is this possible or have Apple now dropped support for this facility.
Any guidance would be gratefully received.

tell application “Pages”
activate
tell document 1
add chart row names {“2008”, “2009”} ¬
column names {“Visual Basic”, “Applescript”} ¬
data {{100, 140}, {80, 200, 220}} ¬
type “vertical_bar_2d” group by “row”
end tell
end tell

As far as I know the command add chart is not defined in modern Pages dictionary.
I guess that you must use make.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 16 juillet 2018 15:29:38

Hi.

I’m afraid you’re out of luck, as far as I can tell. That script was written for Pages ‘09 and has a bug anyway (three numbers instead of two in the second data list). Pages has been to all intents and purposes unscriptable since version 5.0.

When posting AppleScript code here, would you mind wrapping it in our [applescript] and [/applescript] tags? There’s a button for them just above the text window on posting pages. They make the code display thus:

tell application "Pages"
	activate
	tell document 1
		add chart row names {"2008", "2009"} ¬
			column names {"Visual Basic", "Applescript"} ¬
			data {{100, 140}, {80, 200, 220}} ¬
			type "vertical_bar_2d" group by "row"
	end tell
end tell

Thanks for reply (sorry, forgot the

 and 

tags).
If I cannot use Pages, is there an alternative to create a chart?

It looks like Excel’s Applescript Dictionary allows creation of charts.

I think the Smile osax ( http://www.satimage.fr/software/en/downloads/index.html ) still works and has scriptable drawing commands.

In addition to kerflooey: You can also use AppleScriptObjC and create a chart with a couple of NSRects and NSBezierPaths.

I posted some detailed stuff on this back n 2011 as well as some other excel+AppleScript stuff.
If you look at my previous posts’ history you will easily spot it.
Don’t know if subsequent versions of both AppleScript or Excel are now ‘broken’ in this regard, but it used to work fine…

For the record: It’s probably this post.

It turns out it’s possible to create a chart in Pages’s stablemate Keynote, if that’s any help. I haven’t yet found a way to script the result over to Pages.

tell application "Keynote"
	activate
	-- Make a new document whose slides have a white background. There are several on-board themes from which to choose.
	set newDoc to (make new document with properties {document theme:theme "White"})
	tell slide 1 of newDoc
		-- Hide the slide's "Double-click to edit" spiel.
		set {title showing, body showing} to {false, false}
		-- Add a chart with the required details, similarly to how it used to be possible in Pages.
		add chart row names {"2008", "2009"} ¬
			column names {"Visual Basic", "Applescript"} ¬
			data {{100, 140}, {80, 200}} ¬
			type vertical_bar_2d ¬
			group by chart row
		-- Optionally set a variable to the result.
		set newChart to chart 1
	end tell
end tell

Check out these using keynote:

http://piyocast.com/as/archives/2350

http://piyocast.com/as/archives/805