creating txt files from mail

Here is a script that grabs the subject and message of each new email.

i need help getting this script to make a text file of each new email.

the text file needs to be formatted so that the subject is on the first line, and everything else is below it.

the filename also needs to be unique for each message…so i guess it needs to be saved using the date and time as the filename.

any help would be GREATLY appreciated.

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			repeat with eachMessage in theMessages
				set theContent to content of eachMessage
				set theSubject to subject of eachMessage
				my createFile(theContent, theSubject)
			end repeat
		end tell
	end perform mail action with messages
end using terms from

on createFile(theContent, theSubject)
end createFile

I don’t use Mail but you might want to take a look at Mail Scripts, by Andreas Amann. The set includes a script/app named “Archive Messages” which might be close to what you need. Here’s the description:

“Move messages from the selected mailbox(es) to an archive mailbox or export them to standard mbox or plain text files for backup purposes or import into other applications. You can select to move all messages or only messages sent within or certain period as well filter messages based on their read and flagged status.”

– Rob

I tried that, but this is all I could find.

try
	tell application "Finder" to open application file id "com.aamann.MailScripts.ArchiveMessages"
on error number -10814
	do shell script "open -a "Archive Messages.app""
end try