Here is example of using handlers in Pages to create text item object.
- The top level object contains document.
- The document contains page.
- The page contains text
- 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