How to create predefined table in Pages

Hello,

I frequently use a specific table structure in Pages and often need to use it across different documents. Until now, I’ve been copying and pasting these tables, but since I’ve started automating some tasks with Automator, I’m wondering if I can do something similar in Pages.

Is there a way to create a predefined table template in Pages?

Thanks.

1 Like

Create the table you want to re-use. Enter any data (or column headings, etc…) that will want to re-use. Format as desired.

Then, save the file as a template (File > Save as Template…). Pages will offer the option of including your file in the template chooser.

Thank you. I know it, but my goal is not to create a page template. I want to easily insert a predefined table structure at any point in any document.

Currently, I open the file I designated as a template, select the table, and copy it to the desired location, but I want to speed up this process. I am looking whether I can achieve this with a script or macro.

I use tens of e -books (hundreds of pages each) using Pages, but the “table” to be put in the documents is made on Numbers and paste Pages documents.

https://piyomarusoft.booth.pm

By making the “table” of this NUMBERS document in advance, it is enough to copy and paste on Pages documents to adjust the appearance. Isn’t that what you are looking for?

As far as I can tell, Pages won’t let you create a table via AppleScript. It b0rks

tell application "Pages"
	tell document 1
		make new table
	end tell
end tell

Pages got an error: Don‘t know how to create TMAScriptTableInfoProxy

(note, this happens no matter what properties, data, options or whatever you provide the table).

The best you can do is insert a table manually, but then it’s easy to format the table as you like:

tell application "Pages"
	activate
	tell document 1
		set theTable to last table
		tell theTable
			set column count to 5
			set row count to 10
			set header row count to 1
			set header column count to 1
			set width of column 1 to 100
			set width of column 4 to 20
			set height of every row to 30
			set height of row 1 to 20
			set background color of row 1 to {0, 0, 0}
			set text color of row 1 to {65535, 65535, 65535}
			set value of cell "A1" to "Name"
			set value of cell "B1" to "B Header"
			set value of cell "C1" to "Date"
			set value of cell "C2" to (get current date)
		end tell
	end tell
end tell

the biggest challenge comes when trying to index the tables since Pages has some mind-numbing ‘logic’ behind how it tracks tables. I mean, complete, brain-dead, what-the-hell-were-they-thinking kind of logic.

How dumb is it? Try this:

Create a new Pages document. Do nothing to this document other than add 5 empty tables. Then run this script:

tell application "Pages"
	tell document 1
		set t to every table
		repeat with i from 1 to (count t)
			tell table i
				set row count to 1
				set value of cell "A1" to "This is table " & i
			end tell
		end repeat
	end tell
end tell

Before you go any further, consider what you expect to happen. Take a guess.

Now look at what you get compared to your guess, and tell me what the developers were smoking at the time they wrote the table code:

It gets better. Add two more tables to your document and run the script again.

If, like me, you thought you saw a pattern in the original run, your luck just ran out, because this is what you get:

What the actual?

So, in summary, it’s easy to manipulate a table via AppleScript if you can identify it. You can’t create it via AppleScript, and good luck finding it.

– and just for grins, add a new table somewhere in the middle of the mix and see what you get:

:man_facepalming:

OK, I wasn’t prepard to take No for an answer :slight_smile:

Although I hate doing it, I was able to hack a framework running using UI scripting.

The idea is that the script first gets a list of all tables in the current document. It then uses UI scripting (:face_vomiting:) to create a new table. It then compares the list of new tables to the old tables to find what’s new.

Pages does not make this easy. Tables do not have any ID or other unique identifier, so it relies on table names - this should be OK since Pages should create tables with unique names and I’m only looking for a table name that didn’t exist at the beginning of the script, a few milliseconds earlier.

In any case, identifying which is the new table is the hard part. Once you’ve done that you’re free to format it however you like:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Pages"
	activate
	-- get the current document's table list
	set oldTableList to name of every table of document 1
end tell
tell application "System Events"
	tell process "Pages"
		--		make a new table
		click menu item "Basic" of menu 1 of menu item "Table" of menu 1 of menu bar item "Insert" of menu bar 1
	end tell
end tell

tell application "Pages"
	-- now get the current table list
	set newTableList to every table of document 1
	-- find the index of the new table
	set newTableIndex to my findNewTable(oldTableList, newTableList)
	-- bingo!
	set newTable to table newTableIndex of document 1
	-- now we can format it however we like
	tell newTable
		set footer row count to 1
		set header column count to 0
		set row count to 10
		set column count to 4
		set width of column 1 to 60
		set background color of row 1 to {0, 0, 0}
		set text color of row 1 to {65535, 65535, 65535}
		set value of cell "A1" to "Header A"
		set value of cell "B1" to "Header B"
		set value of cell "C1" to "Header C"
		set value of cell "D1" to "Header D"
		set alignment of row 1 to center
	end tell
	
end tell

on findNewTable(oldTablenames, new)
	using terms from application "Pages"
		-- iterate through the new tables
		repeat with i from 1 to count new
			-- do we have a name mismatch
			if (name of item i of new) is not in oldTablenames then
				-- if so, we have our new table
				return i
			end if
		end repeat
	end using terms from
end findNewTable

You can create tables in Pages with applescript. Here is a script that will create (and style) six tables on the current page. You would probably have to tweak it if the page has existing tables.

When I run your test script (from post 5), I get the correct value in each table.

tell application "Pages"
	activate
	tell front document
		tell current page
			set tc to 0
			set y to 72 -- beginning vertical position
			repeat 6 times
				set tc to tc + 1 -- table index
				set tbl to make new table
				set hgt to my formatTable(tbl) -- styles table, returns its height
				set value of cell "A1" of tbl to "is Table " & tc
				set y to y + hgt + 2 -- table spacing
				set position of tbl to {108, y}
			end repeat
		end tell
		-- deselect everything
		tell application "System Events" to key code 0 using {command down, shift down}
		set val3 to value of cell "A1" of table 3 of current page -- test for memory
	end tell
end tell
tell me to activate
display dialog val3 giving up after 3 -- test for memory

-- style table, return table height
on formatTable(tbl)
	tell document 1 of application "Pages"
		tell tbl
			set {header column count, column count} to {1, 3}
			set {header row count, footer row count, row count} to {0, 0, 1}
		end tell
		
		set background color of column 1 of tbl to {52859, 55370, 58046}
		--> {62171, 63679, 60132} -- pale green
		--> {52859, 55370, 58046} -- pale blue
		return height of tbl
	end tell
end formatTable

I do agree that for all but the simplest of tasks, Pages is painful to use and more so to script but at the least, it can do this.

Perhaps, even though the dictionary states that table is an element of document, it isn’t really.

Nioce work, but your script fails to place the tables properly if there are more than 1 page.
I modified it below…

tell application "Pages"
	activate
	tell front document
		tell current page
			set tc to 0
			--set y to 72 -- beginning vertical position
			set y to 0
			repeat 6 times
				set tc to tc + 1 -- table index
				set tbl to make new table
				set hgt to my formatTable(tbl) -- styles table, returns its height
				set value of cell "A1" of tbl to "is Table " & tc
				if y = 0 then
					set y to position of tbl
					set y to (item 2 of y) div 806 * 806 + 72
				else
					set y to y + hgt + 2 -- table spacing
				end if
				set position of tbl to {108, y}
			end repeat
		end tell
		-- deselect everything
		tell application "System Events" to key code 0 using {command down, shift down}
		set val3 to value of cell "A1" of table 3 of current page -- test for memory
	end tell
end tell
tell me to activate
display dialog val3 giving up after 3 -- test for memory

-- style table, return table height
on formatTable(tbl)
	tell document 1 of application "Pages"
		tell tbl
			set {header column count, column count} to {1, 3}
			set {header row count, footer row count, row count} to {0, 0, 1}
		end tell
		
		set background color of column 1 of tbl to {52859, 55370, 58046}
		--> {62171, 63679, 60132} -- pale green
		--> {52859, 55370, 58046} -- pale blue
		return height of tbl
	end tell
end formatTable

The number 806 is the size of each page in points if the page is 8-1/2 by 11

I guess that’s the part I was missing. Good to know!