TextEdit mark text with color

Hello!

I have a text that I use as a to do list. I have structured it over time, and I now use a system like this:
x JOB1: task one
x JOB1: task two
v JOB1: task three
pending JOB2: task one

Using TextEdit feels natural & lightweight and the x-y thing is fairly quick to manage, but I thought it would be nice if the x lines (or paragraphs) where colored in red, and the y lines in green, so a quick glance over the text could tell me what needs to be done.
I’ve found some examples here and there but I seem unable to construct a complete script. Here are two scripts that come close:

TextEdit: Change color of a selection (script from Kai: http://macscripter.net/viewtopic.php?id=20860 )

tell application "System Events" to tell text area 1 of scroll area 1 of window 1 of process "TextEdit" to if exists then
	set {startSelection, endSelection} to value of attribute "AXSelectedTextRange"
	if startSelection ≤ endSelection then tell application "TextEdit" to set color of document 1's characters startSelection thru endSelection to {65535, 3858, 0}
end if
tell application "TextEdit"
	activate
end tell

TextEdit: Search and replace (script from John Steward: http://forum.soft32.com/mac/find-replace-TextEdit-ftopict39169.html )
Cool, but the replace string can’t store color information.


tell application "TextEdit"
	set text of document 1 to my doRplc(get text of document 1, "search string", "replace string")
end tell

on doRplc(txtStr, srchStr, rplcStr)
	set {oldDelims, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {srchStr}}
	set temp to every text item of txtStr
	set AppleScript's text item delimiters to {rplcStr}
	set txtStr to temp as text
	set AppleScript's text item delimiters to oldDelims
	return txtStr
end doRplc

I thought of the following procedure:

  • search for “x (tab) (tab)”
  • if found: get position of file pointer
  • color the paragraph (where the file pointer is in) red
  • go on to the next find of "x (tab) (tab)

Ideal would be a search and replace ‘with rich text formatting’, so one could replace with another color.

I’m sorry to ask this, but I seemed to get stuck here.
Thanks
Elias

Here’s how to set the color of a paragraph… this will make it red…

tell application "TextEdit"
	tell document 1
		set color of paragraph 1 to {65535, 6196, 10546}
	end tell
end tell

An easy way to get a color value is to copy/paste the return value from this script into the above script. First set the text to any color in TextEdit and run this…

tell application "TextEdit"
	tell document 1
		return color of paragraph 1
	end tell
end tell

Thank you regulus,

The ‘set color of paragraph 1 to {65535, 6196, 10546}’ looks good, but what I would like to do is change te paragraph-number to each paragraph starting with the string: “x (tab) (tab)”.

I would use two standard colors, so I could just give static numbers (I found via the code ‘choose color’ that {65535, 0, 0} is for red and {25820, 55453, 10699} is for green).

I understand that. I showed how you can change the color of any paragraph using the paragraph number. So now you need to think of a way to figure out the paragraph numbers.

Try this…

set startText to "x" & tab & tab
set redColor to {65535, 0, 0}
set greenColor to {25820, 55453, 10699}

tell application "TextEdit"
	tell document 1
		set theText to text of it
		set theParagraphs to paragraphs of theText
		
		repeat with i from 1 to count of theParagraphs
			set thisParagraph to item i of theParagraphs
			if thisParagraph starts with startText then
				set color of paragraph i to redColor
			else if length of thisParagraph is greater than 0 then
				set color of paragraph i to greenColor
			end if
		end repeat
	end tell
end tell

Ace!
This is fantastic!

I guess I understand what’s going on. I’ve commented the code a little to my best guess.
It’s fast on small texts, but gets somewhat slower on larger documents

the script looks so simple, yet I could’n find a solution like this.
Maybe it’s just the way to find the code that is difficult, not the script itself in the first place.
Maybe it’s hard to look focussed for a solution.
A better way could be to get more ‘familiar’ by doing lots of tutorials on this site.


-- Script by regulus6633 http://macscripter.net/viewtopic.php?pid=142075#p142075 --
--> initialize variables
set todo to "x" & tab & tab --> define search strings
set done to "v" & tab & tab
set pending to "pending" & tab
set redColor to {65535, 0, 0} --> define colors
set greenColor to {11486, 40347, 6274}
set orangeColor to {57984, 18696, 1944}

tell application "TextEdit"
	tell document 1 --> front document
		set allTheText to text of it --> put all the text of the document into a variable
		set allTheParagraphs to paragraphs of allTheText --> make an array / list contailing all the paragraphs
		repeat with i from 1 to count of allTheParagraphs --> 'for loop' like construction. Loop as many times as there are paragraphs
			set thisParagraph to item i of allTheParagraphs --> assign paragraph number 'i' (increasing loop number) to variable
			if thisParagraph starts with todo then
				set color of paragraph i to redColor
			else if thisParagraph starts with done then
				set color of paragraph i to greenColor
			else if thisParagraph starts with pending then
				set color of paragraph i to orangeColor
			end if
		end repeat
	end tell
end tell
beep

Thanks so much for teaching me!
Elias

Hi Elias, I read your commented script… you understood it perfectly. I’m glad to see that.

If you want to learn more applescript, this site has a bunch of tutorials. You can find them here. When I started I did the ones under the section “Tutorials for beginning scripters”. Good luck.