perl-user perl-user - 13 days ago 4
AppleScript Question

Create a table in Pages

I have a problem I am hoping someone is able to help with...

I am looking to automate the generation of a number of tables containing data within a new iWorks Pages document.

Online there is a wealth of information related to doing this in Numbers, but absolutely nothing to help with doing this within a Pages document and the method seems to differ slightly.

The kind of table I am trying to produce is a simple table with heading columns and then data below e.g.

Column1 Column2 Column3 Column4
data data hello data
moredata moredata data hello
data data data data


It is possible in Microsoft Word by doing:

tell application "Microsoft Word"

set myDoc to make new document

set myTable to make new table at myDoc with properties ¬

{allow auto fit:true, allow page breaks:true, column options:¬

{default width:1.0, preferred width:25, preferred width type:¬

preferred width percent}, number of columns:¬

4, number of rows:12, spacing:2.0}

end tell


But how can it be done in pages?

Your help with this would be greatly appreciated thanks

Answer

Here is an example, this works on the last version of Pages

tell application "Pages"
    set myDoc to make new document
    tell page 1 of myDoc to set myTable to make new table with properties {column count:4, row count:12}
    tell cell range of myTable
        set width of columns to 120
        set text wrap to true
        set alignment to center

        -- to set value of the headers and some cells
        set j to 1
        repeat with i in {"Column1", "Column2", "Column3", "Column4", "data cell 1", "data cell 2", "hello", "data cell 4", "moredata", "moredata", "data", "hello"}
            set value of cell j to i
            set j to j + 1
        end repeat
    end tell
end tell
Comments