Extracting subject line from emlx file

Hi all,

After messing with a number of options to import my mail, I figured just dragging all the emlx messages is easiest, and doesn’t appear to cause any crashes.

Problem is, now I’m stuck with a bunch of .emlx files - not very descrptive.

With some help from previous forum threads on this here forum, I managed to get a script to convert the names of the items from .emlx to the x paragraph of the content.

This works, but only to some extent. I’ve found that for most emails, the third paragraph is the subject line. But sometimes the 3rd paragraph is the content, or it doesn’t exist.

So I have two questions:

  1. Is it possible to extract the subject line more accurately, perhaps some command other than ‘paragraph’?

  2. If not, is there any way to at least not ‘stop’ the script when it runs into an error ‘paragraph 3 doesn’t exist’.?

I’m very new to applescript, and so far I’m very impressed with it (and DT’s scriptability!)


tell application "DEVONthink Pro"
	tell front think window
		set curr to selection
		repeat with r in curr
			set atxt to plain text of r
			set aname to third paragraph of atxt
			set name of r to aname
		end repeat
	end tell
end tell
1 Like

Here’s a script to rename emails (or other stuff with a subject):

tell application id "com.devon-technologies.thinkpro2"
	set theSelection to the selection
	repeat with theRecord in theSelection
			set theMD to meta data of theRecord
			set theSubject to |kMDItemSubject| of theMD
			set name of theRecord to theSubject
		end try
	end repeat
end tell

It works! Thank you VERY much.

I’d like to delve deeper into applescript. Can you give me any idea as to where you found the variables/commands to write this script? I couldn’t find much in the DT library for scripting…

Well, in my brain… I’m one of the developers :wink:

The only trick is accessing the meta data. Possible keys are currently kMDItemTitle, kMDItemHeadline, kMDItemSubject, kMDItemDescription, kMDItemCopyright, kMDItemComment, kMDItemURL, kMDItemKeywords, kMDItemCreator, kMDItemProducer, kMDItemAuthors, kMDItemAuthorEmailAddresses, kMDItemRecipients, kMDItemRecipientEmailAddresses, kMDItemEmailAddresses, kMDItemAlbum, kMDItemComposer, kMDItemContributors, kMDItemPublishers, kMDItemEditors and kMDItemOrganizations.

I’ll add this to the description of the script suite.

Great, thanks!

So, with that metadata I could, say, store the recipients, sender, and date of mail in the comment for the item. Or would it be better to put that somewhere else?

If the comments where to say:

Or even better, I could set the creation date of the item to the send date of the email, right? How would I go about doing that? Would you be willing to help me out on that (setting creation date to email date)?

This information is already stored in the database, see for example Additional Information in the Info panel. And you can search for it by using All or Meta Data options. Therefore I don’t know if there’s a need to add it to the comment.

Ah, I see.

I just noticed that while some metadata is shown in additional information, the email date is not. Any particular reason why this is so?

Oh, and once again thanks for the help. I’ve been playing around with AppleScript and I can’t believe I’ve never used it before. It am awesome.

Why would we add that there? We have for each record: creation date, modified date and added date. The first one is mapped to the received date of the mail message.

That’s Pro Office, right? I’m using Pro, so I manually copy emlx files from ~/library/Mail/etc.

Or am I doing something wrong?

My fault, yes that’s for Pro Office.