Getting text Font & Color from an RTF document in TextEdit - Possible?

I have diddled around at length with a TextEdit problem: I have a document in which particular paragraphs are using fonts, sizes and colors that are different from the bulk text. Because there is a separator, it’s easy enough to single them out and to get their plain text, but I haven’t been able to figure out how to select anything in TE, nor how to determine details of the formatting of a particular paragraph.

Hints, anyone??

A sample paragraph looks like this:

[color=red][b]July 14, 2007[/b][/color] -- about 16 point, but punBB doesn't understand [size=16]Stuff[/size]
Bunch of black text, several paragraphs worth

Next entry

Hi, Adam.

All text elements in TextEdit have color, font, and size properties.

tell application "TextEdit"
	{color, font, size} of paragraph -5 of text of front document
end tell

But only the first format in any paragraph, say, appears to be returned. So if your post above is copy/pasted to TextEdit, the color, font, and size of the line with the red are returned as {65535, 0, 0}, “Verdana-Bold”, and 11.0, even though it also contains dark grey, Geneva text.

A more accurate element is the ‘attribute run’. This is a block of equal formatting, delimited by any change of color, font, and/or size value. It’s more accurate from the point of view of saying: “This text has a certain color, font, and size,” but equating it to a particular paragraph can be fiddly. Of course, you can get the attribute runs of the paragraph itself:

tell application "TextEdit"
	{it, color, font, size} of attribute runs of paragraph -5 of text of front document
end tell
--> {{"July 14, 2007 ", "-- about 16 point, but punBB doesn't understand [size=16]Stuff[/size] ", "
"}, {{65535, 0, 0}, {13107, 13107, 13107}, {0, 0, 0}}, {"Verdana-Bold", "Geneva", "Geneva"}, {11.0, 11.0, 11.0}}

A TextEdit ‘paragraph’ includes the line feed, if present, so the above result shows the red and grey text and the ‘black’ line feed!

I’m sure Kai came up with a GUI Scripting method for setting the selection in TextEdit, but I can’t find it at the moment. I’ll have another rummage around…

Later: No. His method is for finding out the selected range in a document. It’s apparently a read-only process:

tell application "System Events" to tell text area 1 of scroll area 1 of process "TextEdit" to if exists then -- Yuk! (NG)
	set {x, y} to value of attribute "AXSelectedTextRange"
end if

The selection is from before character x to after character y in the text. Selection and insertion point are equivalent here. Selected text is a very wide “insertion point” and any text inserted will overwrite it. If there’s no selection, the value of x will be 1 more than the value of y, because the insertion point is between two adjacent characters and “before x” is the same point as “after y”. :slight_smile:

Bingo, Mr. G. As always, it’s a tiny glitch.

set C to color of paragraph 2 of document 1 fails

set C to color of paragraph 2 of text of document 1 wins. – “of text of” has got to be the only variation I didn’t try. :rolleyes:

Thanks, A