• 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: Shane Stanley <email@hidden>
  • Date: Tue, 05 May 2015 16:36:37 +1000

On 2 May 2015, at 12:20 am, Christopher Stone <email@hidden> wrote:

Ruby's base64 decode looks nearly as simple as quoted-printable:

I don't think you need to go near Base64. I suspect extracting the first quoted printable part and decoding that will return what the OP wants. Something like this:

use scripting additions
use framework "Foundation"

tell application id "com.apple.mail" -- Mail.app
set theSource to source of item 1 of (get selection)
end tell

-- extract first quoted printable section; this could also be done using TIDs or regular expressions
-- make into NSString
set source_s to current application's NSString's stringWithString:theSource
--  start by getting beginning
set headerStartRange to source_s's rangeOfString:(linefeed & "Content-Transfer-Encoding: quoted-printable")
-- boundary string should be preceding paragraph
set theBoundaryRange to source_s's paragraphRangeForRange:(current application's NSMakeRange((headerStartRange's location) - 1, 0))
set theBoundaryString to source_s's substringWithRange:theBoundaryRange
-- find next instance of boundary string to define end of part
set nextBoundaryRange to source_s's rangeOfString:theBoundaryString options:0 range:{current application's NSMaxRange(theBoundaryRange), (source_s's |length|()) - (current application's NSMaxRange(theBoundaryRange))}
-- skip first couple of lines (Content-Type: header)
set headerStartRange to source_s's rangeOfString:(linefeed & linefeed) options:0 range:{current application's NSMaxRange(headerStartRange), (source_s's |length|()) - (current application's NSMaxRange(headerStartRange))}
-- extract range of text
set text_s to source_s's substringWithRange:{current application's NSMaxRange(headerStartRange), (nextBoundaryRange's location) - (current application's NSMaxRange(headerStartRange)) - 1}

-- get rid of QP = new lines; some mailers use CRLF, others LF only
set text_s to text_s's stringByReplacingOccurrencesOfString:("=" & return & linefeed) withString:""
set text_s to text_s's stringByReplacingOccurrencesOfString:("=" & linefeed) withString:""
-- temporarily replace any coded = signs
set text_s to text_s's stringByReplacingOccurrencesOfString:"=3D" withString:"%%%%%%"
-- replace = with %
set text_s to text_s's stringByReplacingOccurrencesOfString:"=" withString:"%"
-- put back = signs
set text_s to text_s's stringByReplacingOccurrencesOfString:"%%%%%%" withString:"="

--decode 
set text_s to text_s's stringByReplacingPercentEscapesUsingEncoding:(current application's NSUTF8StringEncoding)
return text_s as text

You should be able to do all but the last couple of lines using TIDs, regex, whatever.


-- 
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>

 _______________________________________________
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: Christopher Stone <email@hidden>
References: 
 >Re: Scripting Mail: Body content as plain text with quotes (From: Simon Forster <email@hidden>)
 >Re: Scripting Mail: Body content as plain text with quotes (From: Christopher Stone <email@hidden>)

  • Prev by Date: Re: Why can't I set sender in new outgoing Apple Mail message?
  • Next by Date: Re: Scripting Mail: Body content as plain text with quotes
  • 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