Change colors of individual lines in a Numbers cell

Hi:

I’m trying to write an AppleScript that will apply a different color to alternating lines within a cell of a Numbers table.

The cell has a number of lines in it, each separated by an option-return character.

The goal is to make the first line red, the second line blue, the third line red, the fourth line blue, etc.

This can be done manually using the Text Inspector and Text Color controls in the Inspector sidebar. But I’d like to use an AppleScript to automate it.

The following script, for example, when run after selecting text in a cell, uses System Events to change the color of all the text in the cell to blueberry:

activate application "Numbers"

tell application "System Events"
	tell application process "Numbers"
		tell window 1
			tell radio group of toolbar 1
				if value of radio button 1 = {{0}} then
					click radio button 1
				end if
			end tell
			click color well 1 of scroll area 3
			click button 5 of toolbar 1
			click radio button 33 of radio group 1 of splitter group 1
			click button 1
		end tell
	end tell
end tell

(Backticks for code posting added by NG. See the Markdown Reference here).

I’ve tried many things using parts of the script above to try to select the individual lines in a cell and apply a color to them, all to no avail.

Can anyone help me with this?

Thanks in advance.
mm

I do not think you could do that in Numbers directly when value of cell return text.
The problem is if we do:

tell application "Numbers"
	tell table 1 of sheet 1 of front document
		paragraphs of (get value of cell 1)
	end tell
end tell

We get list and not object specifier

So i thought maybe TextEdit could do it. The problem here is that you use option + return.
I was able to style the text in TextEdit and copy/paste the styled text back to Numbers.
Then I thought maybe its possible to convert paragraph in TextEdit after color is set
to use option + return at end of the line.

This is not what you want but it do almost what you want.

tell application "Numbers"
	tell table 1 of sheet 1 of front document
		set theValue to value of cell 1
	end tell
end tell

tell application "TextEdit"
	set theText to make new document with properties {text:theValue}
	tell theText
		set color of paragraph 1 to {65535, 0, 0}
		set color of paragraph 2 to {0, 0, 65535}
	end tell
end tell

copyAllTextFromTextEdit()

tell application "TextEdit" to close front document

tell application "Numbers"
	tell table 1 of sheet 1 of front document
		set value of cell 1 to ""
	end tell
end tell

pasteAllTextToNumbers()

on pasteAllTextToNumbers()
	tell application "System Events" to tell application process "Numbers"
		set frontmost to true
		key code 9 using {command down}
	end tell
end pasteAllTextToNumbers

on copyAllTextFromTextEdit()
	tell application "System Events" to tell application process "TextEdit"
		set frontmost to true
		key code 0 using {command down} -- select all
		key code 8 using {command down} -- copy
	end tell
end copyAllTextFromTextEdit

I was able to do it more simple.

It involve that you click in any cell so the input cursor is blinking inside the cell.
Then you could run the script below. It will make first line to be Red, second:Blue and third:Green

set theText to {"A", "B", "C"}
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
set theText to text of theText as text
set AppleScript's text item delimiters to ASTID

tell application "TextEdit"
	set theObject to make new document with properties {text:theText}
	tell theObject
		set color of paragraph 1 to {65535, 0, 0}
		set color of paragraph 2 to {0, 0, 65535}
		set color of paragraph 3 to {0, 65535, 0}
	end tell
end tell

copyAllTextFromTextEdit()

tell application "TextEdit" to close front document

pasteAllTextToNumbers()

on copyAllTextFromTextEdit()
	tell application "System Events" to tell application process "TextEdit"
		set frontmost to true
		key code 0 using {command down} -- select all
		key code 8 using {command down} -- copy
	end tell
end copyAllTextFromTextEdit

on pasteAllTextToNumbers()
	tell application "System Events" to tell application process "Numbers"
		set frontmost to true
		key code 9 using {command down}
	end tell
end pasteAllTextToNumbers