Pages, makeTextItem, setObjectText, setStyleOfObjectText

Here is example of using handlers in Pages to create text item object.

  1. The top level object contains document.
  2. The document contains page.
  3. The page contains text
  4. The text contains the style of the text

Ps. We use character, word and paragraph to specify the text to style.

(**
* [propertiesTextItem(pageNumber integer, textItemIndex integer)]
*)
-- propertiesTextItem(1, 1)
on propertiesTextItem(pageNumber, textItemIndex)
	tell application "Pages"
		tell page pageNumber of front document
			return properties of text item textItemIndex
		end tell
	end tell
end propertiesTextItem

(**
* [makeNewTextItem(pageNumber integer, objectText string, textPosition list, textSize list)]
*)
-- makeNewTextItem(1, "Hello World!", {75.5, 75.5}, {465, 675}) --> A4
on makeNewTextItem(pageNumber, objectText, textPosition, textSize)
	tell application "Pages"
		set {textWidth, textHeight} to {item 1 of textSize, item 2 of textSize}
		tell page pageNumber of front document to make new text item with properties {object text:objectText, position:textPosition, width:textWidth, height:textHeight}
	end tell
end makeNewTextItem

(**
* [setObjectText(pageNumber integer, textItemIndex integer, objectText string)]
*)
-- setObjectText(1, 1, "MacScripter.net")
on setObjectText(pageNumber, textItemIndex, objectText)
	tell application "Pages"
		tell text item textItemIndex of page pageNumber of front document
			set object text to objectText
		end tell
	end tell
end setObjectText

(**
* [setTextStyleOfObjectText(pageNumber integer, textItemIndex integer, textStyle record)]
*)
-- setTextStyleOfObjectText(1, 1, {font:"Times", size:18, color:{65000, 0, 0}})
on setTextStyleOfObjectText(pageNumber, textItemIndex, textStyle)
	tell application "Pages"
		tell text item textItemIndex of page pageNumber of front document
			try
				set color of object text to color of textStyle
				set font of object text to font of textStyle
				set size of object text to size of textStyle
			end try
		end tell
	end tell
end setTextStyleOfObjectText

(**
* [setTextStyleByParagraphOfObjectText(pageNumber integer, textItemIndex integer, paragraphIndex integer, textStyle record)]
*)
-- setTextStyleByParagraphOfObjectText(1, 1, 1, {font:"Times", size:18, color:{0, 65000, 0}})
on setTextStyleByParagraphOfObjectText(pageNumber, textItemIndex, paragraphIndex, textStyle)
	tell application "Pages"
		tell text item textItemIndex of page pageNumber of front document
			-- If the record do not include any of font, size or color it will not return error.
			try
				set color of paragraph paragraphIndex of object text to color of textStyle
				set font of paragraph paragraphIndex of object text to font of textStyle
				set size of paragraph paragraphIndex of object text to size of textStyle
			end try
		end tell
	end tell
end setTextStyleByParagraphOfObjectText

(**
* [setTextStyleByWordAndParagraphOfObjectText(pageNumber integer, textItemIndex integer, wordIndex integer, paragraphIndex integer, textStyle record)]
*)
-- setTextStyleByWordAndParagraphOfObjectText(1, 1, 2, 1, {font:"Times", size:18, color:{0, 65000, 0}})
on setTextStyleByWordAndParagraphOfObjectText(pageNumber, textItemIndex, wordIndex, paragraphIndex, textStyle)
	tell application "Pages"
		tell text item textItemIndex of page pageNumber of front document
			-- If the record do not include any of font, size or color it will not return error.
			try
				set color of word wordIndex of paragraph paragraphIndex of object text to color of textStyle
				set font of word wordIndex of paragraph paragraphIndex of object text to font of textStyle
				set size of word wordIndex of paragraph paragraphIndex of object text to size of textStyle
			end try
		end tell
	end tell
end setTextStyleByWordAndParagraphOfObjectText

This handler also include character

(**
* [setTextStyleByCharacterAndWordAndParagraphOfObjectText(pageNumber integer, textItemIndex integer, wordIndex integer, paragraphIndex integer, textStyle record)]
*)
setTextStyleByCharacterAndWordAndParagraphOfObjectText(1, 1, 1, 1, 1, {font:"Times", size:18, color:{0, 65000, 0}})
on setTextStyleByCharacterAndWordAndParagraphOfObjectText(pageNumber, textItemIndex, characterIndex, wordIndex, paragraphIndex, textStyle)
	tell application "Pages"
		tell text item textItemIndex of page pageNumber of front document
			-- If the record do not include any of font, size or color it will not return error.
			try
				set color of character characterIndex of word wordIndex of paragraph paragraphIndex of object text to color of textStyle
				set font of character characterIndex of word wordIndex of paragraph paragraphIndex of object text to font of textStyle
				set size of character characterIndex of word wordIndex of paragraph paragraphIndex of object text to size of textStyle
			end try
		end tell
	end tell
end setTextStyleByCharacterAndWordAndParagraphOfObjectText

And if we like to set the font of first character of first word of first paragraph to be Times-Bold

setTextStyleByCharacterAndWordAndParagraphOfObjectText(1, 1, 1, 1, 1, {font:“Times-Bold”, size:18, color:{15000, 25000, 65000}})

Lets do example to check iif text style of characters contains an specific font name.
And return its character index.

(**
* [propertiesTextItem(pageNumber integer, textItemIndex integer)]
*)

set fontStyleCharacters to propertiesOfAllCharactersOfObjectText(1, 1)
set {i, characterIndexList} to {0, {}}
repeat with fontStyle in fontStyleCharacters
	if font of (contents of fontStyle) contains "Times-Bold" then
		set i to i + 1
		set the end of characterIndexList to i
	else
		set i to i + 1
	end if
end repeat
return characterIndexList

on propertiesOfAllCharactersOfObjectText(pageNumber, textItemIndex)
	tell application "Pages"
		tell text item textItemIndex of page pageNumber of front document
			return properties of characters of object text
		end tell
	end tell
end propertiesOfAllCharactersOfObjectText

And now when we have the character index of the boolean test. We could update every character with other font or text style.

set characterIndexes to characterIndexList
setTextStyleFromCharacterIndexes(1, 1, characterIndexes, {font:"Arial Black"})

on setTextStyleFromCharacterIndexes(pageNumber, textItemIndex, characterIndexes, textStyle)
	tell application "Pages"
		tell text item textItemIndex of page pageNumber of front document
			repeat with charIdx in characterIndexes
				try
					set font of character (contents of charIdx) of object text to font of textStyle
				end try
			end repeat
		end tell
	end tell
end setTextStyleFromCharacterIndexes

Fredrik71. Thanks for the scripts.

Just to clarify for people like me with little knowldge of the Pages app, a page can contain a text item (a container) which can contain object text (the actual text) which has various properties. Separately, a page can contain body text (the actual text) which has various properties. At least, that’s the way I understand it.

You are correct, as you know Pages, Keynote and Numbers use the same code base. In other words Apple make 3 applications from it. The defination of body text means different in my world. I would consider to say that text item is some kind of text frame. The frame has position and size. Keynote use the same approach as Notes with title and body.

And thats why I do not use Document body in Pages. (checkbox)
In other words all pasted text become text item that contains text, image… For me that is the best approach to work with content outside Pages. The same approach page layout application are doing things.