Mail returns "???" instead of content of messages with attachments

On some messages in Mail, when I get the content of the message via AppleScript, it just returns “???”. This seems to be restricted to messages that have attachments, but not to ALL messages that have attachments. The only messages it consistently occurs with are those in my Sent folder that I sent from Mail with attachments.

This is the script I’m using to troubleshoot. Pretty basic.

tell application "Mail" to tell first message viewer
	set theMessages to selected messages
	set theContent to content of first item of theMessages
	get theContent
end tell

I have tried setting a type for theContent (e.g. get theContent as Unicode Text or as Text or as String), but that makes no difference.

Anyone know of a fix or work-around for this, or is this yet another fabulous Mail AppleScript bug?

An update: This problem seems restricted to rich text/HTML messages with attachments. If I choose “plain text alternative” (cmd+opt+p), things seem to work fine, regardless of whether or not there are attachments.

Is there a way to just get the plain text portion of an email? I can’t see such an option in Mail’s AppleScript dictionary.

Model: Powerbook G4
Browser: Safari) OmniWeb/v563.60
Operating System: Mac OS X (10.4)

Hi Nik,

You get ??? for attachments because they are placed inline. I have forgotten the workaround, but you can try searching for posts related to this. If I remember, then I’ll write back.

gl,

Hi,

If you want the plain text version of a Mail.app message:

tell application "Mail"
	set myMessage to first item of (get selection)
	set mySource to source of myMessage
	if mySource contains "Content-Type" then
		set myContentType to content of header "Content-Type" of myMessage
	else
		set myContentType to ""
	end if
end tell

if (myContentType contains "multipart/mixed;" or myContentType contains "multipart/alternative;") and (mySource contains "Content-Type: text/plain;") then
	set myBoundary to item -1 of textToList(myContentType, "boundary=")
	if character 1 of myBoundary contains "\"" then set myBoundary to (characters 2 thru -2 of myBoundary) as Unicode text
	set myParts to textToList(mySource, myBoundary)
	set myContent to ""
	repeat with myPart in myParts
		if myPart contains "Content-Type: text/plain;" then
			set myContent to myContent & (listToText(items 2 thru -2 of textToList(myPart, ((ASCII character 10) & (ASCII character 10))), ((ASCII character 10) & (ASCII character 10))))
		end if
	end repeat
else
	tell application "Mail" to set myContent to content of myMessage
end if

return myContent

on textToList(theText, theSep)
	set {myTID, my text item delimiters} to {my text item delimiters, theSep}
	set soFar to text items of theText
	set my text item delimiters to myTID
	return soFar
end textToList

on listToText(theList, theSep)
	set {myTID, my text item delimiters} to {my text item delimiters, theSep}
	set soFar to theList as Unicode text
	set my text item delimiters to myTID
	return soFar
end listToText

Best wishes

John M