I’ve generated a document with a bunch of Heading 1 and Heading 2 entries, and now need to create a Table of Contents at the beginning of the document. There already exists a command to do this, however the additional syntax to do this is a little confusing to me. Looking at the Dictionary here’s what we’ve got.
insert table of contents
This has only one parameter, a direct parameter of type reference.
But of course, once that’s done. How do I get it at the beginning of the document?
So select the point where you want the table to appear and then insert the table in the body text of the document:
tell application "Pages"
tell body text of document 1
select insertion point before paragraph 1
insert table of contents
end tell
end tell
The code is one of the many possible ways in AppleScript of writing:
tell application "Pages"
select insertion point before paragraph 1 of body text of document 1
insert table of contents body text of document 1
end tell