Convert Tex-Edit text color script for TextSoap or TextEdit

I have been using Tex-Edit for years to change the color of words in television scripts… I can find and replace with a color and it saves me hours of time… I am trying to use this apple script that worked in Tex-Edit with TextSoap or TextEdit. I am not versed in scripting so I am trying to get a modification to this to work on a 64bit operating systems (Intel / Apple M1/2 chips)

tell application "TextEdit"
	activate
	if exists window 1 then
		tell window 1
			display dialog "Enter name of talent:" default answer "Enter Name" buttons {"Red", "Cyan", "Green"} default button 1
			copy the result as list to {text_returned, button_pressed}
			
			if button_pressed ≠ "Cancel" then
				try
					if button_pressed is "Red" then
						set color of (paragraphs whose first word is text_returned) to red
					else if button_pressed is "Blue" then
						set color of (paragraphs whose first word is text_returned) to cyan
					else if button_pressed is "Cyan" then
						set color of (paragraphs whose first word is text_returned) to green
						
						
					end if
				end try
			end if
		end tell
	end if
end tell

Here is how I would approach the matter:

tell application "TextEdit"
	set d1 to document 1
	
	set paraList to paragraphs of text of d1
	
	-- button and results	
	set ddr to display dialog "Enter name of talent:" default answer "tvguy" buttons {"Red", "Cyan", "Green"} default button 1
	set {colour, talent} to {button returned of ddr, text returned of ddr}
	
	-- set the colours
	set cred to {{cname:"Red", crcg:{55000, 0, 0}}, {cname:"Cyan", crcg:{0, 55000, 55000}}, {cname:"Green", crcg:{0, 43520, 0}}}
	
	-- get the desired colour
	repeat with c in cred
		if contents of cname of c is equal to colour then
			set rcg to crcg of c
			exit repeat
		end if
	end repeat
	
	-- cycle through paragraphs and colour those matching
	repeat with para from 1 to length of paraList
		if contents of item para of paraList begins with talent then
			set color of paragraph para of text of d1 to rcg
		end if
	end repeat
	
end tell

I used this as my sample text:

tvguy
3hours
tvguy is the guy
I have been using Tex-Edit for years to change the color of words in television scripts

Depending upon your needs, you could probably automate the colouring of the entire script in one go.

Thank you very much… This is what I have been looking for. How can I just do the word PETE: in a color but not the entire line that follows… just the name.

JON:
Why are so many investors now choosing options over stocks?

PETE: It’s simple. Options have the ability to deliver far greater returns on the same security over the same period of time…

JON:
And do it with less capital at risk!

Thank you very much

I’d probably approach it this way:

First, set up a list of character names (charList) and their corresponding colours (colourList). It then cycles through each character and each paragraph to find matches and colours the character name accordingly.

tell application "TextEdit"
	set d1 to document 1
	set ts to text of document 1
	set paraList to paragraphs of ts
	
	-- set up lists of character names and colours	
	set charList to {"Pete", "Jon"}
	set colourList to {{55000, 0, 0}, {0, 55000, 55000}}
	
	-- cycle through characters
	repeat with cc from 1 to length of charList
		set charName to item cc of charList & ":"
		set cl to length of charName
		set colourCode to item cc of colourList
		
		-- cycle through paragraphs
		repeat with para from 1 to length of paraList
			if item para of paraList begins with charName then
				set color of characters 1 thru cl of paragraph para of text of document 1 to colourCode
			end if
		end repeat
	end repeat
end tell

Hi.

Combining @tvguy’s original script with @Mockman’s ideas and a couple of my own:

tell application "TextEdit"
	activate
	if ((count documents) = 0) then return
	
	-- Get speaker and colour input.
	set {text returned:speaker, button returned:colour} to ¬
		(display dialog "Enter name of speaker:" default answer "Enter Name" buttons {"Red", "Cyan", "Green"} default button 1)
	if (speaker does not end with ":") then set speaker to speaker & ":"
	-- Confirm. (Not enough capacity above for a "Cancel" button as well.)
	display dialog "Color instances of the speaker cue \"" & speaker & "\" " & colour & "?"
	
	set speakerLen to (count speaker)
	repeat with c in {{cname:"Red", crgb:{55000, 0, 0}}, {cname:"Cyan", crgb:{0, 55000, 55000}}, {cname:"Green", crgb:{0, 43520, 0}}}
		if (c's cname is colour) then
			set rgb to c's crgb
			exit repeat
		end if
	end repeat
	
	tell text of document 1 to set color of characters 1 thru speakerLen of (paragraphs where it begins with speaker) to rgb
end tell

Nigel, This works perfectly… Thank you very much. Scripts seem to have a 3 color/button limit so I can just change the button and colors and build another script.

I will test this in the field later next week

Cheers
Jeff

An alternative would be to have the list of colour/RGB records conveniently placed at the top of the script, where you can add to it or subtract from it easily, and use choose from list instead of buttons to give a greater choice of the available colours at any one time:

-- List of colour name/RGB records. Edit as required.
property cRecords : {{cname:"Red", crgb:{55000, 0, 0}}, {cname:"Cyan", crgb:{0, 55000, 55000}}, {cname:"Green", crgb:{0, 43520, 0}}}

tell application "TextEdit"
	activate
	if ((count documents) = 0) then return
	
	-- Name of speaker.
	set speaker to text returned of ¬
		(display dialog "Enter name of speaker:" default answer "Enter Name")
	if (speaker does not end with ":") then set speaker to speaker & ":"
	
	-- Colour choice.
	set cNames to {}
	repeat with c in cRecords
		set end of cNames to c's cname
	end repeat
	set cChoice to (choose from list cNames with prompt "Choose a color:" default items {item 1 of cNames})
	-- 'choose from list' returns either 'false' ("Cancel" button clicked) or a list of the chosen item(s).
	if (cChoice is false) then error number -128 -- "User canceled" error.
	set colour to item 1 of cChoice
	
	-- Confirm. (If required.)
	-- display dialog "Color instances of the speaker cue \"" & speaker & "\" " & colour & "?"
	
	-- Find the RGB list corresponding to the colour name.
	set speakerLen to (count speaker)
	repeat with c in cRecords
		if (c's cname is colour) then
			set rgb to c's crgb
			exit repeat
		end if
	end repeat
	
	-- Apply in the document.
	tell text of document 1 to set color of characters 1 thru speakerLen of (paragraphs where it begins with speaker) to rgb
end tell

The “Cancel” button in most AppleScript dialogs generates error number -128 “User canceled.” But for reasons lost in the mists of time, choose from list’s “Cancel” button returns false instead, so this has to be caught and a “User canceled.” error generated explicitly by the script. As an interesting aside, I notice that, since Ventura, on my UK-configured machines, the report of this error in Script Editor’s Result pane has it as “User cancelled.” — ie. British spelling! :sunglasses:

Hi Nigel, This works even better. When the isn’t a colon after someones name, I just hid the & “:” code. My biggest question is where do you find the color codes for the color???

“Green”, crgb:{0, 43520, 0} in the script but the RGB and HEX colors are different numbers. For instance the green I use is 00FF00 and the red is FF0000, Yellow is FFFF00, orange CC6600

Where can I find a decoder?

Cheers
Jeff

Hi Jeff.

You probably just have to experiment a bit until you find something you like. I imagine that’s what Mockman did above. While the number range for each colour in your six-figure hex codes is 00 to FF (0 to 255 decimal), the number range in an AppleScript list representing RGB values is 0 to 65535 (0000 to FFFF hexadecimal). So the values for your green, red, yellow, and orange colours would be somewhere in the region of {0, 65535, 0}, {65535, 0, 0}, {65535, 65535, 0}, and {52428, 26214, 0} respectively. Tweak to taste. :slightly_smiling_face:

Thank you again for your help. I will play with these and figure out the math and understand how to make these numbers work

Cheers
Jeff

I’d guess that when translating a two-digit hex value to an AppleScript integer between 0 and 65535, the integer value can be any number between (hex value) * 256 and (hex value) * 256 + 255. This range includes both (hex value) * 256 and (hex value) * 257, so one of these would be the most efficient choice. A simple translation handler would be:

on hexToRGBList(hex)
	set hexDigits to "0123456789ABCDEF"
	set RGB to {}
	
	repeat with i from 1 to 5 by 2
		set hexHiVal to ((offset of (character i of hex) in hexDigits) - 1) * 16
		set hexLoVal to (offset of (character (i + 1) of hex) in hexDigits) - 1
		set end of RGB to (hexHiVal + hexLoVal) * 256 -- or * 257
	end repeat
	return RGB
end hexToRGBList

hexToRGBList("CC6600")

… which can be reduced slightly to:

on hexToRGBList(hex)
	set hexDigits to "0123456789ABCDEF"
	set RGB to {}
	
	repeat with i from 1 to 5 by 2
		set end of RGB to ((offset of (character i of hex) in hexDigits) * 16 + ¬
			(offset of (character (i + 1) of hex) in hexDigits) - 17) * 256 -- or * 257.
	end repeat
	return RGB
end hexToRGBList

hexToRGBList("CC6600")

You can either use this to get numbers to put into the TextEdit script, or, if you prefer to have your six-digit hex strings in the script, include this handler to translate them on the fly.

Oh this is very cool… Playing with the numbers you gave me and it is working well. Experimenting with numbers that will be readable on screen…