Sending an Email in RTF format

I am trying to send an RTF formatted email from an Xcode AS app. I am using a Text View to enter the text. In the text view object I see all the feature, in the email it is plain text. I get the text using
set theMessage to mMsg’s |string| – mMsg is the NStextView object

The I create a mail message using

tell application “Mail”

make new outgoing message … yada yada yada.
end tell

The mail message shows up as plain text.


If you want the styled contents you need to call textStorage() to get an NSAttributedString – but I don’t know that you will be able to pass that to Mail. You might be able to use RTFFromRange_ and put the RTF on the clipboard, and copy it into Mail.

I do not think the issue is the copy, as I am able to take the whole string from the textView object. The email AS dictionary specifies an object called “outgoing message” which has a property content. When setting content it translates the string to plain text. So the issue is not to copy but to set the content.


Yes, the content property takes a string – that means plain text, no styling. The only way you can get styled text into a message is via copy and paste, or perhaps passing an RTF encoded string. But if you ask for |string| of a text view, you’re losing all the styling at that point anyway.

That makes sense, so how do copy into a mail message via AS?

You’’ probably have to resort to something icky like GUI scripting.

If you can skip Mail, I think there’s a scripting addition around that let’s you send styled emails – that might be a better option.

Do you know where I can find it?

Oh, I’d start looking at :wink:

Okay, I finally figured out a not-elegant but working solution:
Create the email with Mail AS.
Leave the body in the NStextView element.
Use the selectAll and copy methods to put the email body in the clipboard.
use System Events
use keystrokes to bring the cursor to the right location.

It is extremely not elegant, but it works.

Here are some pieces from the code:

– Build the message
tell application “Mail”
set nmsg to make new outgoing message with properties ¬
{sender:theSender, subject:theSubject, content:hhi & return & return}
tell nmsg
make new to recipient at end of to recipients with properties {name:mname, address:maddr}
tell content
if bAttach then
make new attachment with properties {file name:theAttachment} at before the first paragraph
end if
end tell
if (theSignature is not equal to “”) then
set message signature to theSignature
end if
set visible to true
end tell
end tell
– select and copy the message from NStextView
mMsg’s selectAll_(sender)
mMsg’s copy_(sender)
– Define arrows
set leftArrow to ASCII character 28
set rightArrow to ASCII character 29
set upArrow to ASCII character 30
set downArrow to ASCII character 31
– Pasting RTF content
tell application “System Events”
tell process “Mail”
set frontmost to true
keystroke downArrow
keystroke downArrow
keystroke downArrow
keystroke “v” using command down – Paste
keystroke “D” using command down – Send
end tell
end tell