Creating multiple tables throughout a new document in pages

I’ve had a go at this fifteen different ways, and I just can’t figure it out.

The most refined version I can get is by using the add table command:


add table data {{"Data 1", "Data 2"},{"Data3","Data4"}}

However, as I have multiple sections to my document, and I’m creating it section by section, each time I use this command, it creates the table in the same place in the document.

How can I specify its position? Also, how can I use the table class in conjunction with this command?

Many thanks in advance.

Here is the basic scheme to use :


tell application "Pages" to tell document 1
	properties of graphics
	add table data {{"a1", "b1", "c1"}, {"a2", "b2", "c2"}} header column no header row no
	set properties of table -1 to {name:"notable", horizontal position:22.0, vertical position:400.0}
	
	add table data {{"aa1", "bb1", "cc1"}, {"aa2", "bb2", "cc2"}} header column no header row no
	set properties of table -1 to {name:"potable", horizontal position:82.0, vertical position:500.0}
	properties of graphics
end tell

In the log report you will see which properties may be available.
From my point of view, the important point is to give a name to each table created by the script. This way, it will be easy to tell the app which one must be edited.
To understand why it’s useful, run the sample script upon a Pages document already embedding shapes or pictures.
Apple report the value shape as class of a shape object, text box as class of a text box object
but it report graphic for tables, pictures.

Yvan KOENIG (VALLAURIS, France) dimanche 30 décembre 2012 10:11:02

1 Like

Wow! That’s confusing! The Pages dictionary says the ‘data’ parameter for ‘add table’ should be text, whereas it actually has to be a list of lists. The ‘header column’ and ‘header row’ parameters should be booleans, but ‘yes’ and ‘no’ work as well. The ‘horizontal position’ and ‘vertical position’ properties are described as points, but on my machines they appear to be inches. And ‘set properties’ works! :o

It reports the class of a table as a ‘graphic’, but the ‘properties of graphics’ lines in your script both error on my machines. [But see Edit 2 below.] ‘properties of tables’ works.

I’m using Pages 4.1 in Snow Leopard and 4.0.5 in Leopard.

Edit:

As I hoped, it depends on a preference in Pages itself.

tell application "Pages"
	set oldUnits to ruler units
	set ruler units to points
	tell document 1
		add table data {{"a1", "b1", "c1"}, {"a2", "b2", "c2"}} without header column and header row
		set properties of table -1 to {name:"notable", horizontal position:22.0, vertical position:400.0}
		
		add table data {{"aa1", "bb1", "cc1"}, {"aa2", "bb2", "cc2"}} without header column and header row
		set properties of table -1 to {name:"potable", horizontal position:82.0, vertical position:500.0}
		set p to properties of tables
	end tell
	set ruler units to oldUnits
	p
end tell

Edit 2: I’ve just done some tests with a document which Yvan sent to me off list. It turns out that ‘properties of graphics’ does work on my machines after all. The problem was a ‘grouped’ graphic in my own test document which didn’t like being asked for its properties. Ungrouping it stopped the error.

Thanks Yvan, that worked a charm.

I wish to add that, once, the created table wasn’t floating but inline.
So it may be a good idea to add the instruction setting the correct status.


tell application "Pages" to tell document 1
	properties of graphics
	add table data {{"a1", "b1", "c1"}, {"a2", "b2", "c2"}} header column no header row no
	set placement of table -1 to fixed # ADDED
	set properties of table -1 to {name:"notable", horizontal position:22.0, vertical position:400.0}
	
	add table data {{"aa1", "bb1", "cc1"}, {"aa2", "bb2", "cc2"}} header column no header row no
	set placement of table -1 to fixed # ADDED
	set properties of table -1 to {name:"potable", horizontal position:82.0, vertical position:500.0}
	properties of graphics
end tell

Of course, adding the instructions posted by Nigel to guarantee the correct unit is a good idea.
In my original message, the instructions
properties of graphics
were included to let you see that the logic ruling the ordering of objects descriptors is not evident.

If we really need to get properties of graphics, due to the fact (bug ?) that asking for the properties of a group fails, we must use something like :


tell application "Pages" to tell document 1
	set listeProps to {}
	count of graphics
	repeat with g from 1 to result
		try
			properties of graphic g
		on error
			set fakeRecord to {}
			tell graphic g
				try
					shadow
					set end of fakeRecord to {"shadow:", result}
				end try
				try
					alpha threshold
					set end of fakeRecord to {"alpha threshold:", result}
				end try
				try
					horizontal position
					set end of fakeRecord to {"horizontal position:", result}
				end try
				try
					stroke color
					set end of fakeRecord to {"stroke color:", result}
				end try
				try
					locked
					set end of fakeRecord to {"locked:", result}
				end try
				try
					stroke type
					set end of fakeRecord to {"stroke type:", result}
				end try
				try
					opacity
					set end of fakeRecord to {"opacity:", result}
				end try
				try
					placement
					set end of fakeRecord to {"placement:", result}
				end try
				try
					replaceable
					set end of fakeRecord to {"replaceable:", result}
				end try
				try
					name
					set end of fakeRecord to {"name:", result}
				end try
				try
					class
					set end of fakeRecord to {"class:", result}
				end try
				try
					rotation
					set end of fakeRecord to {"rotation:", result}
				end try
				try
					text fit
					set end of fakeRecord to {"text fit:", result}
				end try
				try
					wrap
					set end of fakeRecord to {"wrap:", result}
				end try
				try
					containing page
					set end of fakeRecord to {"containing page:", result}
				end try
				try
					stroke width
					set end of fakeRecord to {"stroke width:", result}
				end try
				try
					id
					set end of fakeRecord to {"id:", result}
				end try
				try
					shadow offset
					set end of fakeRecord to {"shadow offset:", result}
				end try
				try
					width
					set end of fakeRecord to {"width:", result}
				end try
				try
					shadow opacity
					set end of fakeRecord to {"shadow opacity:", result}
				end try
				try
					shadow blur
					set end of fakeRecord to {"shadow blur:", result}
				end try
				try
					extra space
					set end of fakeRecord to {"extra space:", result}
				end try
				try
					height
					set end of fakeRecord to {"height:", result}
				end try
				try
					shadow angle
					set end of fakeRecord to {"shadow angle:", result}
				end try
				try
					shadow color
					set end of fakeRecord to {"shadow color:", result}
				end try
				try
					vertical position
					set end of fakeRecord to {"vertical position:", result}
				end try
				try
					containing layer
					set end of fakeRecord to {"containing layer:", result}
				end try
			end tell
			fakeRecord
		end try
		set end of listeProps to result
	end repeat
	listeProps
end tell

It’s really odd. I assumed that trying to grab the shadow, rotation, text fit,wrap, shadow offset, shadow opacity, shadow blur, extra space, shadow angle, shadow color of a group would issue an error but they don’t.
Errors are issued by :
alpha threshold, stroke color, stroke type and stroke width
name doesn’t issue an error but return nothing so if we want to use the result we get the error “undefined variable”.

Yvan KOENIG (VALLAURIS, France) lundi 31 décembre 2012 10:55:10

Hi Yvan. It’s probably unimportant, but you could save a bit of typing and get a real record like this:

tell application "Pages" to tell document 1
	set listeProps to {}
	count of graphics
	repeat with g from 1 to result
		try
			properties of graphic g
		on error
			set realRecord to {}
			tell graphic g
				try
					set realRecord to realRecord & {shadow:shadow}
				end try
				try
					set realRecord to realRecord & {alpha threshold:alpha threshold}
				end try
				-- etc.

Hello Nigel

It’s important.

I searched a way to build the record and failed.
So, now I have the scheme to achieve what I was trying to do.

Thank you.

Yvan KOENIG (VALLAURIS, France) lundi 31 décembre 2012 15:13:18

Hi. Happy New Year!

A closer look at the add table entry in Pages’s scripting dictionary shows that it takes a direct parameter which is “the object for the command”. In Yvan’s scripts, this is provided by the ‘tell’ statement in which the command appears:

tell application "Pages" to tell document 1
	add table data {{"a1", "b1", "c1"}, {"a2", "b2", "c2"}}

. which is the same as:

tell application "Pages"
	add table document 1 data {{"a1", "b1", "c1"}, {"a2", "b2", "c2"}}

We could extend the current reference in the same way:

tell application "Pages" to tell document 1
	add table page 2 data {{"a1", "b1", "c1"}, {"a2", "b2", "c2"}}

When a table’s manually inserted into a document using the item in Pages’s Insert menu, it appears in a default form at the current insertion point. This behaviour can be exactly reproduced thus:

tell application "Pages" to tell document 1
	add table selection

Experimenting with this may offer an alternative way to insert tables at specific points in a document.

1 Like

And how do you format the cell’s text?