• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Scripting Mail: Body content as plain text with quotes
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Scripting Mail: Body content as plain text with quotes


  • Subject: Re: Scripting Mail: Body content as plain text with quotes
  • From: Christopher Stone <email@hidden>
  • Date: Fri, 01 May 2015 09:20:11 -0500

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:

http://stackoverflow.com/questions/23488404/decode-base64-string-and-write-to-file

Progress.

Need coffee now...

--
Best Regards,
Chris

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Scripting Mail: Body content as plain text with quotes
      • From: Shane Stanley <email@hidden>
    • Re: Scripting Mail: Body content as plain text with quotes
      • From: Simon Forster <email@hidden>
References: 
 >Re: Scripting Mail: Body content as plain text with quotes (From: Simon Forster <email@hidden>)

  • Prev by Date: Rép: Why can't I set sender in new outgoing Apple Mail message?
  • Next by Date: Re: AppleScript date in ISO-8601 ?
  • Previous by thread: Re: Scripting Mail: Body content as plain text with quotes
  • Next by thread: Re: Scripting Mail: Body content as plain text with quotes
  • Index(es):
    • Date
    • Thread