get rich text from clipboard into apple mail outgoing message

I have some rich text formatted text on the clipboard. I want to get that into a new outgoing message in mail. I tried this but I only get plain text in the message instead of rich text…

set richText to the clipboard
set theSubject to "test subject"
tell application "Mail"
	activate
	make new outgoing message with properties {subject:theSubject, content:richText, visible:true}
end tell

So then I tried gui scripting to paste the clipboard which works…

set theSubject to "test subject"
tell application "Mail"
	activate
	make new outgoing message with properties {subject:theSubject, visible:true}
end tell
set myDelay to 0.3
tell application "System Events"
	tell process "mail"
		keystroke tab
		delay myDelay
		keystroke tab
		delay myDelay
		keystroke tab
		delay myDelay
		keystroke "v" using command down
	end tell
end tell

But here’s my questions after my two scripts…

  1. I can’t always be sure that the 3 keystroke tabs will always place the cursor in the body of the email message (before I gui script pasting the rich text) so is there a better way to get the cursor into the message body?
  2. is there a direct way to get the rich text into the message body without using the clipboard or gui scripting at all?

Hi, Hank.

I don’t use Mail my self and don’t have the latest version, but the following works with v2.1 on my Tiger machine provided that RTF composition is set in Mail’s preferences:

tell application "Mail"
	set theSubject to "test subject"
	make new outgoing message with properties {subject:theSubject, visible:true}
end tell

tell application "System Events"
	tell application process "Mail"
		set frontmost to true
		set value of attribute "AXFocused" of UI element 1 of scroll area 3 of window theSubject to true
	end tell
	keystroke "v" using {command down}
end tell

If it doesn’t work for you, maybe it’s adaptable.

Thanks Nigel but it doesn’t work on Leopard. It’s weird because the ui element is referenced the same as you have, but the focus of the cursor never moves from the “to” field where you enter an email address.

I tried this with no luck…

select UI element 1 of scroll area 3 of window theSubject

I even put some text in the content section of the email and tried selecting it like this…

select static text theSubject of group 1 of UI element 1 of scroll area 3 of window theSubject

None of these move the cursor. Any other thoughts?

Presumably you did use the same text for the content as for the subject here?

GUI scripting is notoriously untransportable, even between similar systems. :confused: I don’t know what else to suggest from here. I might experiment with referring to the window by index and with explicitly unfocussing the currently focused field, but not with any great expectations. Sorry. :frowning:

Yes.

I’ll keep trying. If you know of any way to do it from a cocoa method let me know. I’m trying to do this from a cocoa application I’m building. I want to take the NSAttributedString (i.e. rich text) from an NSTextView, open a new email, then put the rich text in as the body. I figured applescript was the only way to do this, so I place the attributed string on the pasteboard in cocoa, open the new email message with applescript, all I need now is a way to get the pasteboard into the email’s body… or of course a direct way to do this using objective-c but I couldn’t find a way with that. I don’t see any way to control Mail.app using obj-c.

Thanks for the help and Merry Christmas!

I got it to work! At least in 10.5 it works with Mail.app v3.5. I haven’t tested it with anything else yet but now I have hope! The fix was simple. To change the focus to the body of the new email message I just needed to change this line…

set value of attribute “AXFocused” of UI element 1 of scroll area 3 of window 1 to true

to:

set value of attribute “AXFocused” of scroll area 3 of window 1 to true

So basically I just needed to remove “of UI element 1”. Thanks for the help Nigel, I can now paste rich text from the clipboard into a new email message. Of course I’ll test this more with other versions of Mail.app but if I run into a problem a simple if/then statement should allow me to adjust the applescript as needed.

Great news! :slight_smile:

I think your solution may work on my system upstairs too. I’ll check it out when I get an opportunity.