Script to Save Messages from Mail

WIth Andreas Amann “Archive Messages” Mail Script currently not working with Tiger, I’m trying to create a script that will save the selected Mail messages in individual text files.

I’m using Tex-Edit Plus to save the messages. And the script works fine – but only for the first message in the list. When it hits the second message, I get the following error as the AS tries to save it thru TexEdit Plus: “Tex-Edit Plus got an error: File some object wasn’t found.”

Any ideas as to what I’m doing wrong. Thanks.

global source_folder
set the source_folder to choose folder with prompt "Please select destination folder."

tell application "Mail"
	set the_messages to selection as list
	repeat with a_Message in the_messages
		set theSender to sender of a_Message
		set theSubject to subject of a_Message
		set theDate to date received of a_Message
		set theContents to content of a_Message
		if (count of characters in theSubject) > 36 then
			set sub_name to (characters 1 through 36 of theSubject) as text
			set sub_name to theSubject as text
		end if
		my process_save(theSender, theSubject, theDate, theContents, sub_name)
	end repeat
end tell

on process_save(theSender, theSubject, theDate, theContents, sub_name)
	tell application "Tex-Edit Plus"
		make new document
		tell document 1
			make paragraph at end with data (theSender & return & return & theSubject & return & return & theContents & return & "--end message--")
			save in file ((source_folder as text) & (((my convert_month_of(theDate) as text) & "_" & (day of theDate) as text) & "_" & year of theDate as text) & "_" & (sub_name as text))
			close saving no
		end tell
	end tell
end process_save

on convert_month_of(theDate)
	set this_month to the month of theDate
	set month_listA to {January, February, March, April, May, June, July, ¬
		August, September, October, November, December}
	set month_listB to {"01", "02", "03", "04", "05", "06", ¬
		"07", "08", "09", "10", "11", "12"}
	repeat with i from 1 to the number of items in month_listA
		if this_month is item i of month_listA then
			set this_month to item i of month_listB
			return this_month
		end if
	end repeat
end convert_month_of

Did the second message have a subject line starting with “Re:”, by any chance?

If so, that would explain the problem - since the script uses a message’s subject as part of the filename. As I’m sure you’re aware, colons can’t be used in filenames - and Tex-Edit Plus was probably looking (within the selected folder) for a folder called “Re” (the object that “wasn’t found”).

There’s also another potential problem, which may not have manifested itself yet: If more than one message with a particular subject line is selected, then the file containing the first message will be overwritten with the text from the second one.

One final point: Do you really want to use Tex-Edit Plus for this job? Much as I like the application, I’d personally opt in this case to file messages more ‘quietly’, as in the following example (which replaces colons in filenames with dashes, and ensures that each message is filed with a unique name):

on monthNumber(d) (* based on NG's development of the 'French Vanilla' method *)
	tell d's year to if it < 1904 then set d to d + 3.78683424E+10
	copy d to c
	set c's month to January
	(((d - c + 266917392) div 2629728) as string)'s text 2 thru 3
end monthNumber

on cleanName(t)
	tell t to if (count) > 36 then set t to text 1 thru 36
	if ":" is in t then
		set d to text item delimiters
		set text item delimiters to ":"
		set t to t's text items
		set text item delimiters to "-"
		set t to t as string
		set text item delimiters to d
	end if
end cleanName

on uniqueFile(f)
	set x to f
	set n to 1
	tell application "Finder" to repeat while exists file x
		set n to n + 1
		set x to f & " (" & n & ")"
	end repeat
end uniqueFile

to saveToFile(m)
	set f to uniqueFile(m's f & monthNumber(m's d) & "_" & day of m's d & "_" & year of m's d & "_" & cleanName(m's t))
	set c to m's s & return & return & m's t & return & return & m's c & return & "--end message--" as string
	set d to open for access f with write permission
	write c to d
	close access d
end saveToFile

set f to (choose folder with prompt "Choose a target folder:") as Unicode text
tell application "Mail" to repeat with m in (get selection)
	tell {f:f} & m's {s:sender, t:subject, d:date sent, c:content} to my saveToFile(it)
end repeat

Wow. Right on the money. I did end up figuring out the colon issue and wrote and handler to take care of that. But you’re absolutely right – the TexEdit is a lazy solution because I couldn’t remember how to write data directly to a file. Thanks for the help. Now if I could only figure out how to deal with messages with attachments…