On May 01, 2015, at 06:12, Simon Forster <email@hidden> wrote:… I’m looking for the easy way.
______________________________________________________________________
Hey Simon,
I'm PDS there's not one, but if you prove me wrong I'd like to know.
Oh well, time to grab the source of the message, dig out the Content-Type header and, where appropriate, boundary and then find the text/plain version.
--------------------------------------------------------------------- tell application "Mail" set selMsgList to selection if selMsgList ≠ {} then set selMsg to item 1 of selMsgList tell selMsg set contentType to content of first header whose name is "content-type" set AppleScript's text item delimiters to {linefeed & linefeed} set msgBody to (text items 2 thru -1 of (get source)) as string end tell end if end tell ---------------------------------------------------------------------
Now you can play with quoted-printable. :)
If you find a good lib for decoding it please let me know.
Just seems silly ‘cause Mail must have done it already.
I hear you.
Hmm... Ruby it seems has a built-in method for dealing with QP:
------------------------------------------------------------------------------ #! /usr/bin/env ruby
msgText = ' > On 1 May 2015, at 03:23, Christopher Stone = <email@hidden> wrote: >=20 > On Apr 30, 2015, at 05:31, Simon Forster <email@hidden> wrote: >>=20 >> Does anyone know of a relatively easy way to get the body content of = an email message with quote levels in place? > ______________________________________________________________________ >=20 > Hey Simon, >=20 > Simple? It depends on the content. :) >=20 > Try getting the source of the message.
Headers, multi-part MIME, etc. Of course it can be parsed out but I=E2=80=99= m looking for the easy way.
:-)
Oh well, time to grab the source of the message, dig out the = Content-Type header and, where appropriate, boundary and then find the = text/plain version. Just seems silly =E2=80=98cause Mail must have done = it already.
ATB
Simon= '
puts msgText.unpack "M" ------------------------------------------------------------------------------
At the moment I don't know how to put the Ruby and the AppleScript together, but I'll fiddle with that some more after I have breakfast.
Got stubborn and didn't go eat yet.
This works after a fashion on quoted-printable - so far from a Content-type: multipart/mixed message (pun intended).
I still have to cover base64 and figure out the subtleties, but I've been wanting to do this anyway.
If someone who knows Ruby wants to demonstrate how to improve on that I'd be grateful.
------------------------------------------------------------------------------------------- tell application "Mail" set selMsgList to selection if selMsgList ≠ {} then set selMsg to item 1 of selMsgList tell selMsg set contentType to content of first header whose name is "content-type" set AppleScript's text item delimiters to {linefeed & linefeed} set msgBody to (text items 2 thru -1 of (get source)) as string end tell end if end tell
# Playing with getting quoted printable. set msgBody to fndUsing("(?m)Content-Transfer-Encoding: quoted-printable.*?\\n{2}(.+)" , " \\1" , msgBody, 0, 1) of me set msgBody to cng("(?m)\\s*--Apple-Mail=.+", "", msgBody) of me
set tempFile to POSIX path of ((path to temporary items from user domain as text) & "msgBodyTemp")
# Writing the file to avoid messing with quoting. writetext msgBody to POSIX file tempFile # Satimage.osax
set shCMD to text 2 thru -1 of " /usr/bin/ruby -e' fileRef = File.open(\"" & quoted form of tempFile & "\", \"rb\") msgText = fileRef.read fileRef.close puts msgText.unpack \"M\"'" do shell script shCMD
------------------------------------------------------------------------------------------- --» HANDLERS { Both require Satimage.osax } ------------------------------------------------------------------------------------------- on cng(_find, _replace, _data) change _find into _replace in _data with regexp without case sensitive end cng ------------------------------------------------------------------------------------------- on fndUsing(_find, _capture, _data, _all, strRslt) try set findResult to find text _find in _data using _capture all occurrences _all ¬ string result strRslt with regexp without case sensitive on error false end try end fndUsing -------------------------------------------------------------------------------------------
Somewhere there's probably an email.decode module that you just feed the source and get output, but I haven't gone looking yet.
Ruby's base64 decode looks nearly as simple as quoted-printable:
Progress.
Need coffee now...
-- Best Regards, Chris
|