Mail.app: Export selected message as .txt file

Following script exports selected message as plain text file,
in the same way you save selected message manually thru menu “Save As…” >>> Plain Text File.
.


-- Mail.app: Export Selected Message to plain text (.txt)  file 
-- written: by me, right now

use AppleScript version "2.5"
use scripting additions
use framework "Foundation"

set timeZone to (abbreviation of (current application's NSTimeZone's localTimeZone())) as text

-- Mail.app part (load remote content, get message properties)
tell application "Mail"
	set download html attachments to true -- load remote content as well
	tell (item 1 of (get selection)) -- here we tell to 1st selected message
		set {theSubject, fromHider} to {subject, "From: " & sender}
		tell (get date sent) -- build here "Date: ....." text line
			set dateSentHider to "Date: " & date string & " - " & time string & " " & timeZone
		end tell
		set {toHider, messageContent} to {"To: " & address of to recipient 1, content}
	end tell
end tell

set allText to fromHider & linefeed & "Subject: " & theSubject & linefeed & dateSentHider & ¬
	linefeed & toHider & linefeed & linefeed & messageContent

-- replace every ":" symbol in theSubject with "_"
set {ATID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
set {itemList, AppleScript's text item delimiters} to {text items of theSubject, "_"}
set {theSubject, AppleScript's text item delimiters} to {itemList as text, ATID}

-- build destination file's Posix path, write to it
set theFile to POSIX path of (path to desktop folder) & theSubject & ".txt"
-- or,
-- set theFile to POSIX path of (choose folder) & theSubject & ".txt"
(current application's NSString's stringWithString:allText)'s writeToFile:theFile atomically:true ¬
	encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)