Script for merging with rich text strips formatting?

Long story short, I’m importing emails into DT, converting to RTF to add highlighting, and then I later need to merge each conversation’s emails to end up with one long RTF, complete with highlighting. I want to do this with a script rather than Merge as I want to automatically set the document title and eventually reset the date to the first email date.

The script below works great, with one problem - it loses the formatting and highlighting. The resulting document is an RTF document, but with plain text in it, and I can’t figure out what I’m doing wrong. Any clues please?

Here’s the script:

tell application id "com.devon-technologies.thinkpro2"
		set theGroup to create location "Emails Merge Test"
		set theSelection to the selection
		set theName to ""
		set theContent to "" as styled text
		set blankLines to (return & return) as styled text
		repeat with theItem in theSelection
			if theName is "" then
				set theName to "PR Email - " & name of theItem
			end if
			set theContent to theContent & (rich text of theItem) & blankLines
		end repeat
		set theRecord to create record with {name:theName, type:rtf, rich text:""}
		set the rich text of theRecord to theContent
		move record theRecord to theGroup
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
	end try
end tell


Someone from Devon could comment more authoritatively, but it looks like asking DTPO for the “rich text” of a database item returns the text stripped of RTF codes. Where this happens along the line (or whether this is “incorrect” behavior) I can’t tell.

I’d suggest getting the text directly from the files and concatenating them. Keep in mind that RTF is a text format, so your script doesn’t need to deal with “styled” text or “rich” text. But therein lies trouble.

For example, your approach is to concatenate RTF files in their entirety, so if you cat 5 RTF files, you’re going to get 5 RTF preambles in your final file. This will give unexpected results when you interpret the RTF.

Also, I didn’t run your script, but is the “PR email” tag being handled correctly? It looks like it sets the name of the selected database items, but doesn’t get used as a separator in the final RTF file. Keep in mind that you’ll have to correctly format this separator in RTF to get the results you expect.

It may be simpler to script TextEdit or Text Edit Plus to do all the labor with RTF via cut-n-paste.

HTH, Charles

Even Leopard’s support for rich text scripting is close to no support as the formatting is lost as long as you don’t copy visible (!) rich text within the same application, e.g. from one window to another.

Apologies for the delay in getting back to this thread - thanks for the replies!