How do you write formatted text to Pages 5.5.3?

In the past, I wrote some little routines that helped me in my writing with Pages 4.3, and it worked very well for me. However, I have tried numerous times to make the conversion to Pages 5.5.3, with no success. Can anyone help me with this?

The following routine works fine in Pages 5.5.3:

–======================================================================

–Script Starts Here
–para1
set para1Text to “This is the first paragraph.” & return

–para2
set para2Text to “This is the second paragraph.” & return

–para3
set para3Text to “This is the third paragraph.” & return

–para4
set para4Text to “This is the forth paragraph.” & return

tell application “Pages”

activate

#Create a new Pages document using a template "Blank," if available.
set the installedTemplates to the name of every template
if installedTemplates contains "Blank" then
	set currentDocument to make new document with properties {document template:template "Blank"}
else
	display dialog "Error:  The Blank Template Is Not Installed."
end if

tell body text of currentDocument
	
	-- First we create a line of text using whatever style we happen to have at this time.
	set paragraph 1 to para1Text
	
	-- Now we create a second line of text using whatever style we happen to have at this time.		
	set paragraph 2 to para2Text
	
	-- Now we create a third line of text using whatever style we happen to have at this time.		
	set paragraph 3 to para3Text
	
	-- Now we create a forth line of text using whatever style we happen to have at this time.		
	set paragraph 4 to para4Text
	
	-- Now lets see if we can add text to the end of paragraph 2.		
	set paragraph 2 to paragraph 2 & "  Here is an added sentence."
	
	-- Now lets makes some changes to paragraph 3.		
	tell paragraph 3
		set the color of it to "red"
		set the size to "18"
		set the font to "Helvetica"
	end tell

	-- So far, so good.  HOWEVER:
	
	-- Now lets makes some more changes to paragraph 3.		
	tell paragraph 3
		--set the alignment to "center"
		--set bold to "true"
	end tell

	-- That doesn't work, lets try something else:
	
	-- make new paragraph style with properties {name:"ParagraphThree", font name:"Palatino", font size:24, bold:true, alignment:center}
	
end tell

	-- That doesn't work either.

end tell

–Script Ends Here
–======================================================================

The issue comes when I try to set the alignment and font style. Take note of the code at the bottom:

	tell paragraph 3
		--set the alignment to "center"
		--set bold to "true"
	end tell		

If I remove the comments, the script fails. How is one supposed to set the alignment and font style in Pages 5.5.3?

Something like this does not work either:

make new paragraph style with properties {name:“ParagraphThree”, font name:“Palatino”, font size:24, bold:true, alignment:center}

Any help is greatly appreciated.

To treat this kind of problem, the first thing to do it to examine the properties of existing items with the wanted attributes.

Here, I created a fake document with 3 paragraphs:
the 1st one is in regular Helvetica.
The second is in bold Helvetica
the 3rd one ic centered in italic Helvetica.

then I ran this short script :

tell application "Pages" to tell document 1 to tell body text
	properties of paragraphs
end tell

The result was :
{{font:“Helvetica”, color:{0, 0, 0}, class:placeholder text, size:11},
{font:“Helvetica-Bold”, color:{0, 0, 0}, class:placeholder text, size:11},
{font:“Helvetica-Oblique”, color:{0, 0, 0}, class:placeholder text, size:11}}

It appear that, as we may see in the dictionary, there is no property describing the style or the alignment.
To put a piece of text in bold or in italic, we just need to apply it the correct font.

Remember that iWork components doesn’t create fake styled characters as the sh. named Word does.
If you are using a font which is unavailable in bold version in your system you will be unable to apply it the bold property.

To be short.

To have a paragraph in bolded Helvetica you just need to set its font to “Helvetica-Bold”.
This mean that you must know the name of the variants of the fonts which ou want to use.
It’s a bit annoying.

As there is no property to set the alignment we may look at GUI Scripting.
Assuming that a paragraph is selected and that the Text pane is active on the right edge of the window

tell application "Pages" to activate
tell application "System Events" to tell process "Pages"
	set frontmost to true
	tell window 1
		log (get class of UI elements)
		--> (*splitter group, button, button, button, static text, menu button, toolbar, static text*)
		log (get class of UI elements of splitter group 1)
		--> (*scroll area, static text, button, button, button, button, button, button, splitter, radio group, button, button, radio group, scroll area*)
		log (get class of UI elements of scroll area 2 of splitter group 1)
		--> (*static text, static text, pop up button, pop up button, group, incrementor, text field, button, color well, static text, group, group, group, UI element, static text, pop up button, UI element, static text, button, scroll bar*)
		log (get class of UI elements of group 1 of scroll area 2 of splitter group 1)
		--> (*checkbox, checkbox, checkbox, checkbox*)
		log (get accessibility description of UI elements of group 1 of scroll area 2 of splitter group 1)
		-->	(*gras, italique, souligné, options avancées*)
		log (get class of UI elements of group 2 of scroll area 2 of splitter group 1)
		--> 	(*checkbox, checkbox, checkbox, checkbox*)
		log (get accessibility description of UI elements of group 2 of scroll area 2 of splitter group 1)
		--> (*à gauche, centré, à droite, justifié*)
		log (get class of UI elements of group 3 of scroll area 2 of splitter group 1)
		--> (*checkbox, checkbox, checkbox*)
		log (get accessibility description of UI elements of group 3 of scroll area 2 of splitter group 1)
		-->	(*en haut, au milieu, en bas*)
		log (get class of UI elements of group 4 of scroll area 2 of splitter group 1)
		--> (*checkbox, checkbox*)
		log (get accessibility description of UI elements of group 4 of scroll area 2 of splitter group 1)
		--> (*Diminuer le retrait, Augmenter le retrait*)

		# Bingo, now we know what to do to apply bold to the selected paragraph
		click checkbox 1 of group 1 of scroll area 2 of splitter group 1

		# Wee know also what to do to center the selected paragraph
		click checkbox 2 of group 2 of scroll area 2 of splitter group 1
	end tell
end tell

Yvan KOENIG (VALLAURIS, France) lundi 4 mai 2015 10:43:59