how to keep mail source and attachments together as textwhen exporting

I would like to be able to grab the contents of an email message and dump it to a text file.

When I try using something like
tell application “Mail” to set msg_source to source of thisMessage
the variable msg_source contains the body but not the attachments.

I have been able to put together something which limps along using UI scripting:

local localFileName, localDirName, dTime
set localDirName to "/Users/whomever/Documents/Scratch"
set localFileName to "eraseme" -- needs to be garbage collected at the end
set dTime to 2
tell application "Mail" to set selectedMessages to the selection
set thisMessage to the first item of selectedMessages
tell application "Mail" to activate
tell application "System Events"
	tell application process "Mail"
		keystroke "s" using {command down, shift down} -- Save As... doomed in Mountain Lion?
		delay dTime
		--		keystroke "g" using {command down, shift down}
		--		delay dTime
		keystroke localDirName -- starts with an / so the cmd-shift-g not needed
		delay dTime
		keystroke return
		delay dTime
		keystroke localFileName
		delay dTime
		keystroke return
		delay dTime
		keystroke tab using shift down -- these need to be replaced with the proper UI element
		delay dTime
		keystroke tab using shift down
		delay dTime
		keystroke tab using shift down
		delay dTime
		keystroke tab using shift down
		delay dTime
		keystroke space
		delay dTime
		keystroke "r" -- make sure 'raw text' is selected
		delay dTime
		keystroke return
		delay dTime
		keystroke return
	end tell
end tell

This is a really fragile and lame solution, because the UI scripting forces me to sit and twiddle my thumbs if the script is running through a long series of messages.

Anyone have anything better which does not rely on perl or other external applications?

Hello!

I’d have a look if I were you in the dictionary of mail.app, There you will find mail attachment as a property of a message. I think you should try to get the name of this attachement, and see what is rendered, if you are in luck, it is a path to the folder it is downloaded into!

If you are in luck, you would make a folder for each message, and dumpt the text and attachment into that folder, to have it all in place.

I really need to preserve the email as it was, ugly mime divisions, mime encoding, and everything, so that I have one text file which matches the way that the file was sent to me.

The attachments which are properties of the email can be saved as separate files in their proper binary format. This is useful most of the time, but not for my particular purpose. The file I’m trying to get is a raw text file.

I really know to little about this, though the mail headers is a topic I am very interested in.

I don’t even know if the attachments are encoded into the mail, or sent as separate files, if you understand where I am heading at. Either way, I think you will have to download the attachment from the mail, to get at its contents.

I hope memory serves me wrong when I seem to recall that I had to go into a property list file, to find the relations between a mail attachment, and the id of the message it was coming from. :frowning:

But if the attachment is encoded into the mail, i.e into the same piece of message as the text, and identified by some kind of identifier there, (I don’t know how the mail format is constructed), then I think what you are saying is that you want the mail message in its raw unformatted form?

Yes, exactly: I need the mail message in its raw unformatted form.

The raw unformatted form is what gets saved by File → Save As… and then choosing ‘Raw Message Source’ as the format in the resulting dialog box.

One would hope that because Mail.app can save raw source files via a dialog box, it should be able to do the same thing via Applescript. My experience is, however, that Applescript dictionaries are a hodgepodge, and rarely reflect everything an application can do.

Hello!

I think you are in luck, as a message has a source property! :slight_smile:

If you don’t have anything saving the raw message to a file, you should easily find something around here!

HTH

Saving the source saves just the text of the message and not the attachments (see the original post). This is the root of the problem.