View Single Post
The export .docx route does have the advantage of retaining character formatting like bold/italic, but if a script is what you need, you should be able to do it with something like the following.

(Bear in mind that Pages uses the MS Word convention of only having header styles down to level 9, whereas 003 indents indefinitely. In this draft script anything beyond 9 becomes body text by default – I notice that the docx exporter flattens all further indentation to level 9, incidentally. I prefer to use body text, because it offers at least a distinction between levels 9 and 10 of indentation.

If you edit the first line of the code to:

property pblnBodyAfterLevel9 : false

It will create styles with names like "Heading 10" etc.
but the outliner won't indent them, and it's up to you to provide definitions for them.)


Code:
property pblnBodyAfterLevel9 : true

tell application id "OOut"
	if (count of documents) < 1 then return
	set {lstLevel, lstText} to {level, topic} of rows of front document
end tell

tell application id "page"
	activate
	set oDoc to make new document
	set outline visible of front window to true
	tell body text of oDoc
		repeat with i from 1 to length of lstLevel
			set lngLevel to item i of lstLevel
			set oStyle to my GetStyle(oDoc, lngLevel)
			properties of oStyle
			make new paragraph at end with data item i of lstText with properties {paragraph style:oStyle}
		end repeat
	end tell
end tell

on GetStyle(oDoc, lngLevel)
	tell application id "page"
		tell oDoc
			if lngLevel ≤ 9 then
				return paragraph style ("Heading " & lngLevel)
			else
				if pblnBodyAfterLevel9 then
					return paragraph style "Body"
				else
					set strLevel to "Heading " & lngLevel
					try
						return paragraph style strLevel
					on error
						return (make new paragraph style with properties {name:("Heading " & lngLevel)})
					end try
				end if
			end if
		end tell
	end tell
end GetStyle

Last edited by RobTrew; 2012-01-10 at 04:34 AM.. Reason: Added a switch to choose between "Body" and "Heading 10" etc beyond level 9