Change text colour of specific words in Numbers cells with AppleScript?

Hi,


I'm trying to make an AppleScript that allows me to change the colour of specific words in cells in Numbers (not the entire cells). I've tried it in Pages, where words are easily found and changed, but in a Numbers cell it doesn't seem to work the same way.


In my present case it's always the last word in the sentence that I want to change.


on run {input, parameters}
	set testVal to "newcard"
	set testCol to 2 -- column B
	set newColor to {0, 0, 0}
	tell application "Numbers"
		tell front document's active sheet
			tell (first table whose selection range's class is range)
				repeat with r in rows 1 thru -1 -- skip header row
					if r's cell testCol's value contains testVal then
						-- set r's background color to backgroundColor1
						-- set r's cells's text color to textColor1
						
						-- set text color of testVal to newColor
						set text color of (r's cell's value as string)'s last word to newColor
					else
						-- do something else
					end if
				end repeat
			end tell
		end tell
	end tell
	
end run


I left some commented code that I used to test that everything else works. I can change cell background colour and the color of the entire cell text, where the word resides – just not isolated words.


I get the error message: "Syntax error. Can’t set «class texC» of "newcard" to {0, 0, 0}."



What am I doing wrong? :)


Thanks in anticipation!


Palle



MacBook Air 13″

Posted on Jan 20, 2023 08:36 AM

Reply

Similar questions

6 replies

Jan 20, 2023 10:01 AM in response to palle.tv

If you look in the AppleScript Dictionary for Numbers (in Script Editor, File > Open Dictionary ...) you will see that 'text color' is a property of a 'range of cells' in a table. Same with 'background color'.




You get an error where your script treats 'text color' not as a property of a 'range of cells' but rather as the property of the value of a cell.


As far as I can tell it is not possible via AppleScript to set the color for just a word of the text in a cell. It's all or nothing.


Text boxes ('text item' in AppleScript) should behave more like Pages, as should shapes.


For example this works:


set color of last word of object text of text item 1 to "red"



SG


Jan 20, 2023 01:23 PM in response to palle.tv

You can manually paste multicolor text into a cell. Maybe you can bring TextEdit or Pages into the mix in your script. Have it create the multicolor text in one of those apps then GUI script copy/paste it into a cell. Kind of clunky but might work. Or maybe with a temporary text box or shape within Numbers, which would keep it all within one app.

Jan 20, 2023 04:42 PM in response to palle.tv

palle.tv wrote:

no underlying XML or other comprehendible source code to work with. Only CSV and TSV that have no formatting options.


Right, you have probably discovered that text in Numbers cells is rich text but from AppleScript you have less control of it than you do of object text (rich text in objects). And (like CSV/TSV) plain AppleScript is not good at getting rich text in and out. AsObjC might help if you really, really need this and are willing to get down in the weeds, but I doubt it because of the limitations in Numbers support for AppleScript. In plain AppleScript formatting generally is lost when you assign a value to an AppleScript variable. So you have to go through the clipboard, which entails clunky gui-scripting that is notoriously fickle (and slow).


SG

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Change text colour of specific words in Numbers cells with AppleScript?

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