• 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 18:09:37 +1000

On 5 May 2015, at 5:31 pm, Christopher Stone <email@hidden> wrote:

You're assuming there is a quoted-printable part.

That's true. I'm also assuming UTF-8, which is not a good assumption.

I get some messages that are entirely base64 in the source.

Assuming you have parsed out the relevant part of data and know it's text, the rest is actually easier than quoted printable:

set theNSData to current application's NSData's alloc()'s initWithBase64EncodedData:text_s options:(current application's NSDataBase64DecodingIgnoreUnknownCharacters)
set theString to current application's NSString's alloc()'s initWithData:theNSData encoding:((current application's NSUTF8StringEncoding) as text)

My experience with QP is that a few find/replaces fails to be comprehensive enough.

That's where stringByReplacingPercentEscapesUsingEncoding: should help. Anyway, here's an updated version of the last script, dealing with both utf-8 and iso-8859-1 encoding:

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 contentStartRange 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 header text
set headers_s to source_s's substringWithRange:{current application's NSMaxRange(headerStartRange), (contentStartRange's location) - (current application's NSMaxRange(headerStartRange))}
-- get encoding
set encodingRange to headers_s's rangeOfString:"charset=\\S+" options:(current application's NSRegularExpressionSearch)
set theEncoding to headers_s's substringWithRange:{(encodingRange's location) + 8, (encodingRange's |length|()) - 8}
if (theEncoding's isEqualToString:"iso-8859-1") as boolean then
set theEncoding to current application's NSISOLatin1StringEncoding
else
set theEncoding to current application's NSUTF8StringEncoding
end if
-- extract range of text
set text_s to source_s's substringWithRange:{current application's NSMaxRange(contentStartRange), (nextBoundaryRange's location) - (current application's NSMaxRange(contentStartRange)) - 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:theEncoding
return text_s as text

-- 
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>)
 >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: Christopher Stone <email@hidden>)

  • Prev by Date: Re: Scripting Mail: Body content as plain text with quotes
  • 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