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.