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:
