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