Change words' color in Pages meeting certain conditions with AppleScript. Words include arithmetic signs

I am writing a programming materials. I would like to highlight some keywords in it meeting certain conditions.


Code block:

int x = 10;
int x = (int) d;
// int, double, float, ...


Would like to change a color of word "int" and equal sign (=), provided that word "int" is followed by a space or is surrounded by brackets. So in line 3 there is no need to change color.


In the case of brackets only the word "int" should change color, not brackets.


The following script does not change the color of any character:

set wordList to {"int ", "=", "<="} 

set theColor to {0, 51 * 257, 179 * 257}

tell application "Pages"
	tell body text of front document
		repeat with aword in wordList
			set color of (words where it is aword) to theColor
		end repeat
	end tell
end tell
```


Is it possible to do the task with AppleScript? Thanks a lot for your help.


2) And a second related question. Is this possible to pass a color in HEX?


set theColor to (HEX)


[Re-Titled by Moderator]

Mac mini

Posted on Apr 14, 2025 9:56 AM

Reply
Question marked as ⚠️ Top-ranking reply

Posted on Apr 14, 2025 12:59 PM

Oh boy, you picked a doozy :)


At first glance, your script doesn't work because of the definition of a 'word' in Pages (well, in AppleScript in general).


Notably, if you ask for 'words of' the document, you get:


{"int", "x", "10", "int", "x", "int", "d", "int", "double", "float"}


Compare that to your wordList and you'll see the first problem:


set wordList to {"int ", "=", "<="}


By definition, 'words' in AppleScript are alphanumeric, do not include spaces, punctuation or other symbols, so you're first checking for 'int ' (note the trailing space), which will never match AppleScript's definition of a 'word' which will always drop the trailing space.

You could change wordList to include 'int' rather than 'int ', but even then you're not out of the woods, because AppleScript's 'words' do not include "=", "<=", etc., so these are excluded.


(side note: there is a little known/used considering/ignoring clause where you can tell AppleScript to consider or ignore white space, diacriticals, and other modifiers, but it still doesn't apply here).


It gets worse, though. Even assuming you could get Numbers to break down the text into space-delimited chunks, the code:


			set color of (words where it is aword) to theColor


is doomed to failure. That's because words where it is aword will return something like:


{"int", "int", "int", "int"}


because those are the literal words that match your search term - you asked for 'words where it is 'int", and that's what you get. There's no reference back to the document at that point, so Numbers has no way to change the text's color.


So, what it comes down to is that Numbers really isn't geared to make this kind of change for you. It is possible to do, but only by walking through the document.


Here's a revision to your script that 'walks' the characters and determines which 'words' to change color:


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

-- 'words' to change color
set wordList to {"int", "=", "<="}

set theColor to {0, 51 * 257, 179 * 257}

-- tokens that break words
set symbolTokens to {" ", ";", return, "(", ")", ","}

tell application "Pages"
	-- get the body text
	tell body text of front document
		-- get all the characters
		set allChars to its characters
		-- start at the beginning
		set startChar to 1
		--iteratate through the characters		
		repeat with i from 1 to count allChars
			-- do we have a word break?
			if item i of allChars is in symbolTokens then
				-- determine the word we have just found
				set checkWord to (characters startChar through (i - 1)) as text
				-- is it one of our keywords?
				if checkWord is in wordList then
					-- then change it
					set color of characters startChar through (i - 1) to theColor
				end if
				-- and move on
				set startChar to i + 1
			end if
		end repeat
	end tell
end tell


You can see the lists at the beginning which identify the words to colorize, as well as the characters that indicate a word break. Amend these as you like.


Additionally challenges I can foresee include comments (different color?), and there are probably others.


> 2) And a second related question. Is this possible to pass a color in HEX?


Sure, but only if you do the decoding of hex to decimal :)


Pages (and AppleScript) define a color as a RGB triplet of three values 0..65535, so if you want to define the color as a Hex triplet (e.g. 0xFFBB00) then you'll have to do the conversion before you pass it to Pages.

16 replies
Sort By: 

Change words' color in Pages meeting certain conditions with AppleScript. Words include arithmetic signs

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.