Merge with automatic titles

Does anyone happen to know if this is possible…?

Lets say I have a series of emails sorted into folders automatically, so they’re already grouped.

I’d really like to merge the emails into a single document for each conversation.

Selecting the files and using right-click > Merge does the trick, but it calls it ‘18 merged documents’ instead of the thread title.

Is there any way of using the name of the folder, or the name of the first selected post, to name that new document? I could do each individually, but I’ve got thousands to try to do!

Ok, I think I’ve made some progress, but I’m stuck.

I’ve modified the Merge script (which throws an error, btw) and come up with this:

tell application id "com.devon-technologies.thinkpro2"
	activate
	try
		set theGroup to create location "Emails/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 name of theItem
			end if
			set theContent to theContent & (plain 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

So that’s doing most of what I want, except I can’t get it to retain rich text from the email, whereas the Merge command in the right click menu does.

The line that says:


set theContent to theContent & (plain text of theItem) & blankLines

only works with plain text, not rich text. Any ideas?

And how do I select the parent of the current folder, rather than setting a specific folder?

Thanks for your help!