Going to a set paragraph in a Pages document

I would like to know the AppleScript syntax to reach a certain pagraph before the last in a Pages document.

For example, if I wish to reach the paragraph before the last I can use:

tell application "Pages"
	activate
	tell the front document
		tell body text
			
			tell the first word of the paragraph before the last paragraph
				set the color of it to "blue"
				set the size to "18"
				set the font to "Times New Roman Bold"
			end tell
			
		end tell
	end tell
end tell

However, if I wished to do so for the second paragraph before the last paragraph or the second paragraph after the first paragraph I get a syntax error. (For example:

tell the first word of the second paragraph before the last paragraph

or

tell the first word of paragraph two before the last paragraph

Could someone let me know what the correct syntax is?

The last paragraph of the body text is paragraph -1. The second to last is paragraph -2. Etc etc.

So…

tell application "Pages"
	tell document 1
		tell body text
			tell paragraph -2
				tell word 1
					set {its color, size, font} to {"blue", "18", "Times New Roman Bold"}
				end tell
			end tell
		end tell
	end tell
end tell

Does the job.

Edited - needed an “its” before “color”.

Thank you very much for the information.

1 Like