Mail.app: Help saving as plain text

For a long time I was running with Barebones Softwares Mailsmith and I was pretty happy with it. But I have recently had to switch to Mail.app and I would be just as happy with it but for one problem.

I keep an archive of what we call Question of the Week http://wanderingknight.org/glhqotw?getqotw=y for an email list hosted on Yahoo. The archive is updated with an applescript that would run as part of Mailsmith mail filters and add the messages as they came in. I re-wrote the script to work with Mail instead which really I thought would just mean changing terms to get the message sender’s name and email address, date sent, and message contents… When I test ran the script (it seems to crash Mail when run attached to a rule, more on this later, it follows the standard format of Using terms From “Mail”…on perform mail action with messages…end…end) it pulls the proper information and writes out a file, but the file it writes out is not a plain text document. I need it to be plain text (suitable for future Perl scripts to deal with). How do I make sure a plain text file is written out?

When I pull my informaiton from the message I have tried coercing it all with “as string” E.g.,

set myContents to content of msg as string

But that has not made a difference. Here is the guts of the script. As you can see a lot of additional text processing is done and this works the way it should. Thanks for any help you can give.

Oh yes, if you visit the link mentioned above you’ll see how messages are being mangled now. If you check a previous week you’ll see how they should look.


			tell application "Mail"
				set myFrom to extract name from sender of msg as string
				set myFromEmail to extract address from sender of msg as string
				set myContents to content of msg as string
				set theMetadate to ("meta-creation_date: " & date sent of msg as string) & return
				
				-- get the meta-title (the subject without and Re:'s and anything after " (was" )
				set theMsgTitle to the my trim_line(subject of msg, "Re: ", 0) as string
				if ((offset of listId in theMsgTitle) > 0) then
					set theMsgTitle to text ((length of listId) + 1) thru (length of theMsgTitle) of theMsgTitle
					--					display dialog theMsgTitle
				end if
				if (theMsgTitle contains " (was") then
					set AppleScript's text item delimiters to " (was"
					set the_items to every text item of theMsgTitle
					set theMsgTitle to item 1 of the_items as string
					set AppleScript's text item delimiters to ""
					--set theMsgTitle to my trim_line(theMsgTitle, space, 1)
				end if
				set theMetaTitle to "meta-title: " & theMsgTitle & return as string
				if the subject of msg contains "[qotw]" then
					set theMetaTangent to "meta-tangent: false" & return
				else
					set theMetaTangent to "meta-tangent: true" & return
				end if
				if (theMetaTangent contains "false") then
					set extraString to ""
				else
					set extraString to " " & the theMsgTitle & ".tangent"
				end if
				set theMsgTitle to theMsgTitle & return as string
				set AppleScript's text item delimiters to "------------------------"
				set the_items to every text item of myContents
				set body to item 1 of the_items
				
				-- now remove text after a person's name at the, hopefully the end, of the message
				set messageEndList to {return & myFrom, return & first word of myFrom, return & "Siler", return & "JTW", return & "Pathfinder", myFrom & return, first word of myFrom & return, "Siler" & return, "JTW" & return, "Pathfinder" & return} as list
				try
					repeat with messageEnd in messageEndList
						set AppleScript's text item delimiters to messageEnd
						set the_items to every text item of body
						--display dialog (messageEnd)
						if (number of items of the_items > 1) then
							set body to item 1 of the_items & messageEnd
							if (messageEnd does not end with return) then
								set body to body & return
							end if
							exit repeat
						end if
					end repeat
				on error
					set AppleScript's text item delimiters to ""
					-- main message didn't end with person's name
				end try
				try
					set footer to item 2 of the_items
				on error
					set footer to ""
				end try
				if "[Non-text portions of this message have been removed]" is in body then
					set AppleScript's text item delimiters to "[Non-text portions of this message have been removed]"
					set the_items to every text item of body
					set body to item 1 of the_items
				end if
				set AppleScript's text item delimiters to ""
			end tell
			
			set theFileContents to "Response from " & myFrom & space & return & theMetadate & theMetaTangent & theMetaTitle & return & body & return & myFromEmail
			set theFilePath to GetUniqueFileName(pselectedFolder & BuildPartialPath(), fileNumber, extraString)
			tell application "Finder"
				set theFile to (open for access file theFilePath with write permission)
				write theFileContents to theFile
				close access theFile
				set theFile to file named theFilePath --for some reason, the original file reference doesn't work
				set creator type of theFile to pCreatorType
			end tell

Ken:

I apologize for not having time to examine your entire script. It sounds as though, however, all you need to do is get the pertinent information from Mail, and you are off and running. I believe that I can assist in that endeavor.

Whenever you tell Mail to get a message, it always returns a list, even though it is often a list of a single item. That is most likely why you are not getting the data you want. For instance, this script will extract the sender, subject, and content of a selected message:


tell application "Mail"
	set theSelection to selection
	tell item 1 of theSelection
		set theSubject to subject
		set theSender to sender
		set theContent to content
	end tell
end tell

I hope this helps you get things rolling along.

No, Cas, that doesn’t work. That’s what the script was doing. The problem was it was saving the text as Unicode. I found a hack to convert to plain text and that mostly solved the problem. The hack is is:

set theFileContents to «class ktxt» of ((theFileContents as text) as record)

What bus me about things like this is that I had no idea this class existed and I still don’t know how to find out about classes like this (so would love to know the answer).

Once that conversion is done I still had to do a find/replace for linefeed characters so I could replace them with carriage returns. But I kind of figured I’d have to do that anyway.

Ahhh, now I see. Sorry about that. I am still a neophyte at all this Unicode/ASCII/Plain Text issue anyway. From what little I have read, supposed Unicode is supposed to save the world. Eventually.

Good luck to you, and congratulations on finding the conversion command.