I’m working on the Mail portion of a little Text-to-Speech AppleScript app and I need to strip the headers out of the message body before speaking the email. Does anyone know how to get just the body content of an email from Mail.app?
I’m using this right now:
set theBody to (source of oneMessage as text)
and it gives me full headers along with the message body.
“content” works great for non-HTML messages or HTML message that include a text-only version. I was hoping to support HTML only messages (HTML stripped) as well.
This will give you the entire source of the e-mail including all headers and HTML tags:
tell application "Mail"
set selectedMessages to selection
set theBody to (source of item 1 of selectedMessages as text)
say theBody with default_voice and waiting until completion
end tell
This will give you the text content of the e-mail without the headers and HTML tags:
tell application "Mail"
set selectedMessages to selection
set theBody to (content of item 1 of selectedMessages as text)
say theBody with default_voice and waiting until completion
end tell
If there is a “remove headers” script floating around out there, I’d love to use it.
Ok, with thanks to Andreas Amann I’ve got this working:
tell application "Mail"
set selectedMessages to selection
set theBody to (source of item 1 of selectedMessages as text)
set theBodyList to {}
set AppleScript's text item delimiters to {"
"}
set theBodyList to text items 2 thru (number of text items in theBody) of theBody
set AppleScript's text item delimiters to ""
set theBody to ""
repeat with snippet in theBodyList
set theBody to theBody & snippet
end repeat
say theBody with default_voice and waiting until completion
end tell
i have a similar question … but related to text-based attachments. for some reason i don’t understand, if attachments are ‘text’, mail.app displays them within the body of the email rather than as attachments. i would like to write an applescript to save the text attachment part to a file (maybe default it to '~/mail_attachments).
here is the full ‘raw source’ of the ‘header/body’ info … would anyone know how i could script this up and use it within mail.app as a ‘rule’?
(the email body text is first … the text attachment is second … it’s different fromt he first as it has the ‘Content-Description’ and ‘name=‘file’’ descriptors … i’d like to be able to preserve the file name if possible)