Apple Pages—change font color with

Hello!

macOS Sequoia 15.5
Pages 14.4

My goal is to build an AppleScript script that changes the color of currently selected text in Pages’ front window.

Pages’ AppleScript dictionary defines selection as follows.

document n [see also Standard Suite, Pages Suitel
PROPERTIES
selection (list of iWork item) : A list of the currently selected items.

However, in Script Editor the following script does not validate “selection.”

tell application “Pages”
activate
try
if not (exists document 1) then error number 1000
tell document 1
set theSelection to selection of document 1
set color of theSelection to color “Maraschino”
end tell
end try
end tell

Likewise if I replace “Maraschino” with {64764, 10794, 7196}.

How do I fix this script so that it functions successfully?

Please edit your post to put your code in between lines with ```, like so:

your code here 

It makes it much easier to work with. Thanks.

I am using an older version of Pages that lacks the ability to work with a selection so I can’t help you with that, although ‘iwork items’ generally suggests images, text boxes or other movable objects rather than integral text on the page.

That said, here is some code that formats an arbitrary block of text. Try it with a new document.


tell application "Pages"
	
	set body text of document 1 to "script that changes the color"
	
	set size of words 3 thru 5 of body text of document 1 to 24
	set color of words 3 thru 5 of body text of document 1 to {64764, 10794, 7196}
	
end tell

A couple things to note:

  1. Your code nests directions to document 1. Get rid of either the tell command or the of document 1 specifier. As it is, you are specifiying selection of document 1 of document 1.
  2. Remove the try statement until you get the script working as it will hide any responses that might let you know what is actually happening
  3. Unless Pages has changed, you must use colour in the number list form, ie as you have it below your code
  4. When you are changing the text of the document, you must refer explicitly to that text — you cannot specify a variable. If you use a variable there, the set color command will be directed at the variable rather than the actual text of the document.

So maybe try something like:

set color of selection of document 1 to {64764, 10794, 7196}

If I’m right about the iwork container stuff, then you could only really work with text inside a text box. Also, I think the selection refers to the entire box rather than text parts within.

Try adding a text box and entering some text within it. Then run this command:

set color of object text of text item 1 of document 1 to {64764, 10794, 7196}

Hi Mockman,

Oops, regrets for my goof. Will do in the future.

Same symptom when using set color of selection 1 of document 1…

tell application "Pages"
	activate
	try
		if not (exists document 1) then error number 1000
		set color of selection of document 1 to {64764, 10794, 7196}
	end try
end tell

Script Editor returned the following error message, and again “selection” was not validated.

error number -1700 from color of selection of document 1 to specifier

Very puzzling.

The other script in your posting “script that changes the color” functioned without returning an error message, but the text in the new (frontmost) document did not change color or font size.

I anticipate that, even if my AppleScript script did validate, and even if it executed without an error, the font’s color would not change. My understanding of Pages is that text style takes priority over character style in AppleScript scripting.

Regardless, I’m just trying to eliminate the issue of “selection” not validating.

The last script only works with the text inside a ‘text box’, made using the ‘insert > text box’ menu item. Type some text in it and then try the script again. A ‘text box’ is an ‘iwork item’, as are tables, lines, charts, etc…

As mentioned, I don’t have a current version of Pages and my version has no mention of ‘selection’. If you look in the dictionary for Pages, can you find any other instances of ‘select’?

Finally… typically, a higher specifity will be prioritized when it comes to handling multiple styles. That is, a character style will override a paragraph style. So if you have a paragraph of blue text but make one word red, then you’ll see both formats within the paragraph. Otherwise, you would have to create a style for any deviation from the norm.

The style selector inside the format inspector further demonstrates this by adding an asterisk to the style name when that red text is selected (along with changing the colour of the style name in the inspector). Click on the drop-down triangle to see the style list, and then hover the mouse over the style in question. You should then see a ‘>’ to the right of the active style. Click on that symbol and a sub-menu will appear that lets you manage the style conflict or even add a keyboard shortcut.

FWIW, perhaps that is a way to approach your problem.

Thanks again for your posting.

I understand your suggestion about using a text box. However, my goal is to change the font color of selected text that is within the body of a page, not text within a text box.

Yes, I saw that you do not have the current version of Pages.

Pages’ AppleScript dictionary has only the following two entries for the terms “select” and “selection.”

selection Property iWork Suite A list of the currently selected items.
selection range Property iWork Suite The cells currently selected in the table.

“Section” (rather than “selection range”) looks like the appropriate one for my goal.

In my test document, the entire paragraph text is “Body” (without an asterisk) and that style has only one color—black.

Thanks again for your time and postings.

At least in the current Pages’ ( v.14.2 ) ‘selection’ is a reference to the current document. Which itself contains, obviously, a ‘selection’ property which is a reference the current document. Recursively, ad infinitum. Well done Pages team!

I would give up on this approach. You could GUI script this but it’s not supportive of this at all.

Make your selection, and this code will open the “Text Color” color-picker window…

tell application "System Events"
	tell its application process "Pages"
		tell its window "Untitled 20"
			tell its splitter group 1
				tell its scroll area 2
					tell color well 1
						perform action "AXPress" -->( "Text Color" window opens )
					end tell
				end tell
			end tell
		end tell
	end tell
end tell

Unfortunately, the color-picker window is only present when the Pages app is frontmost. Making exploring it tedious with Script Debugger. This will give you the elements and you can extend the return statement to get sub-elements.

tell application "Pages" to activate

tell application "System Events"
	tell its application process "Pages"
		tell its window "Text Color"
			return entire contents
			-->{splitter group 1 of window "Text Color" of application process "Pages", slider 1 of splitter group 1 of window "Text Color" of application process "Pages", value indicator 1 of slider 1 of splitter group 1 of window "Text Color" of application process "Pages", slider "Opacity" of splitter group 1 of window "Text Color" of application process "Pages", value indicator 1 of slider "Opacity" of splitter group 1 of window "Text Color" of application process "Pages", static text "Opacity" of splitter group 1 of window "Text Color" of application process "Pages", text field "Opacity" of splitter group 1 of window "Text Color" of application process "Pages", splitter 1 of splitter group 1 of window "Text Color" of application process "Pages", color well 1 of splitter group 1 of window "Text Color" of application process "Pages", checkbox 1 of splitter group 1 of window "Text Color" of application process "Pages", scroll area 1 of splitter group 1 of window "Text Color" of application process "Pages", list "favorite swatches" of scroll area 1 of splitter group 1 of window "Text Color" of application process "Pages", toolbar 1 of window "Text Color" of application process "Pages", button 1 of toolbar 1 of window "Text Color" of application process "Pages", button 2 of toolbar 1 of window "Text Color" of application process "Pages", button 3 of toolbar 1 of window "Text Color" of application process "Pages", button 4 of toolbar 1 of window "Text Color" of application process "Pages", button 5 of toolbar 1 of window "Text Color" of application process "Pages", button 1 of window "Text Color" of application process "Pages", button 2 of window "Text Color" of application process "Pages", group 1 of button 2 of window "Text Color" of application process "Pages", group 1 of group 1 of button 2 of window "Text Color" of application process "Pages", button 3 of window "Text Color" of application process "Pages", static text "Text Color" of window "Text Color" of application process "Pages")
		end tell
	end tell
end tell

I cannot seem to SET the value of the color well, but

tell application "Pages" to activate

tell application "System Events"
	tell its application process "Pages"
		tell its window "Text Color"
			return the value of every color well of item 1 of UI elements -->"rgb 0 0 0 1"
		end tell
	end tell
end tell

Just to clarify… I was not suggesting that you use a text box.

I was simply explaining that the selection property exists only for text boxes (and other iwork items). There is no selection property for body text.

Also, regarding the asterisk… When you apply a format style to some text and then change the styling of part of that text, then an asterisk will appear to inform you that there is now a style conflict.

So for example, if you select a paragraph and apply the ‘Body’ style, and then select one word in the paragraph and colour it red, then while that word is selected, the format style will read ‘Body*’. This is to let you know that although the selection has a format style, it has also been changed in some way.

I was trying to explain that you can change the format of already-styled text. When you do so, then the first two items of the contextual menu will not be greyed out.

Hi.

I don’t know if this is any use to you. It’s a bit of a fudge and, as written, only works for body text. It’s also desirable that the keyboard isn’t touched while its running!

(* This script uses scripted keystrokes. *)

on setColorOfSelectionInPages(RGB)
	set unlikelyText to "§" -- Must be typeable with the current keyboard.
	
	-- Get the front document's body text.
	tell application "Pages"
		activate -- Pages must be frontmost to receive the keystrokes below.
		set t1 to front document's body text
	end tell
	
	-- Overwrite the selected text or insertion point with the unlikely text.
	tell application "System Events"
		set frontmost of application process "Pages" to true -- Overkill.  :)
		keystroke unlikelyText
		delay 0.2
	end tell
	
	-- Wait a maximum of 1 second until the text has changed in the document and get the result.
	-- The script will tolerate the selected and unlikely texts being the same,
	-- but it may not be the selected instance that's recoloured!
	tell application "Pages"
		set t2 to front document's body text
		repeat 5 times
			if (t2 ≠ t1) then exit repeat
			delay 0.2
			set t2 to front document's body text
		end repeat
	end tell
	
	-- Undo the change in the document.
	tell application "System Events" to keystroke "z" using command down
	
	-- The selection in the document runs from the offset of the unlikely text in the second version of the text …
	set s1 to (offset of unlikelyText in t2)
	-- … for the length of the unlikely text minus the difference in length between the two versions.
	set s2 to s1 + (count unlikelyText) - 1 + (count t1) - (count t2)
	-- Set the colour of that range. if more than 0 characters.
	if (s2 ≥ s1) then
		tell application "Pages" to set (front document's body text's characters s1 thru s2)'s color to RGB
	end if
end setColorOfSelectionInPages

setColorOfSelectionInPages({64764, 10794, 7196})

Edit: Bug fixes.

I did try a method which copies the selected text to the clipboard, uses ASObjC methods to change its colour, and pastes it back into the document. But although the colour change on the clipboard was apparently successful (as demonstrated by pasting the text into TextEdit), the text was still the same colour when pasted back into Pages. I may investigate this further at more leisure.

Hi Paul,

Many thanks for your posting/scripts.

I ended up using Keyboard Maestro to build a macro for each of four text colors.

The macro opens the Color Picker, moves the pointer to the desired color’s crayon based upon an image of the crayon, then clicks the mouse to change the selected text in the frontmost Pages document to that crayon’s color.

Hi Nigel,

Many thanks for your posting and script.

Before my first posting in this thread, I also tried the copy selected text to clipboard, modify the text there, paste back and got the same results as indicated in your posting.

FWIW I think the text remains the same/original color because the style’s color takes priority over the pasted text’s color.

Create a character style: select text, colour and format as desired, click the ‘character style’ drop down in the Format Inspector and then the ‘+’ button. Then name it and assign a keyboard shortcut to it by hovering the cursor over the ‘>’ symbol and clicking. At the bottom of the contextual menu will be ‘Shortcut’. Hover over it and select a function key to use as the shortcut. Presumably, you can assign this to KM.

Repeat for your other colours and save in a template.

Pages’ help has some explanation around styles; there are various kinds.

I have made a screen recording of the steps involved.

h264.mp4.zip (721.6 KB)