How do I get RAW source from Mail?

This script saves the raw source of an email from Mail.app:


set tmpPath to (path to home folder) as string

tell application "Mail"
	set theSel to selection
	set theMsg to (item 1 of theSel)
	set theSource to source of theMsg
	
	set rawF to (open for access file (tmpPath & "foo.txt") with write permission)
	set eof rawF to 0 --empty anything previously in the file
	write theSource as string to rawF
	close access rawF
end tell

The problem is, the file is different from the one you would get if you choose “File|save as” in Mail.app and choose to save the raw message source in the options of Save dialog.

(especially emails with non-western encodings)

What should I do to get the absolute RAW source saved as a file using AppleScript? Thanks!

So it turns out “source of theMsg” is of class Unicode Text. Coercing it into a string is fine, but the result is not what I want (as my last post said the file produced is different from the one saved from Mail.app).

I’ve been trying to coerce it into International Text, thinking maybe it’s possible to extract just the data part of it and save it into a file. Two questions:

  1. How do I actually do that?
  2. I’ve tried to do “set theSource to source of theMsg as International Text”, but I got a very weird error message: “Can’t make Can’t make international text into a some object”. That’s right, it’s even not grammatical! So what am I missing here?

Thanks!