Hello to everybody,
I don’t know if I can post this question in this forum but I hope that someone can help me! Is three days that I shake my head but I can’t find a solution!
I’m trying to create and send an e-mail from a Cocoa application using Scripting Bridge! The e-mail body comes out from a template.txt file placed in the Bundle! All works fine! But now I would like to set the content of the mail message as a rich text! And I don’t know how!!!
This is my code:
Using an NSAttributedString as class of the content istance, the body in the mail message doesn’t appears!
If there is an easyest solution in Applescript can someone show me?
Applescript doesn’t allow you to set the message as rich text directly. As you’ve seen only plain text works. I have found a work-around although it’s not very elegant. Basically all you can do is put your attributed string on the pasteboard (clipboard) and then activate mail, move the cursor into the body of the email, and issue a paste command.
Firstly thanks for your reply!
Yours solution is good … but I don’t solve completely my problem because I’ve the intention of create an html formatted message! A sort of newsletter page!
Ok! So if I find another way to do this, I’ll post it!
Thanks StephanK,
but when I try to set the content of the MailOutgoingMessage instance to an attributed string, the new message shows up without body but only with the signature! This because the property “content” accepts only NSString class!!!
I didn’t know you wanted html… you said rich text. Here’s a method I wrote to send an html email. It actually uses safari to create the email! That’s the only way I found to do it.
-- create an html email in Mail.app
-- this will manually create a html file and then use safari to actually create the html email in Mail.app
set receiver to "somebody@email.com"
set theSubject to "This is the subject"
set myHTML to quoted form of "<html>
<body bgcolor=\"#ff8000\">
<p>
This is a paragraph. <b>This is a bold paragraph.</b>
</p>
<p>
This is another paragraph.
</p>
</body>
</html>"
-- create a temporary html file
set tempFile to (path to temporary items folder as text) & "temp.html"
do shell script "echo " & myHTML & " > " & quoted form of POSIX path of tempFile
-- open the html file in safari, then use safari to open that in mail
tell application "Safari"
activate
open file tempFile -- open the html file in safari
tell application "System Events" to keystroke "i" using command down -- open the html in mail.app
activate
tell application "System Events"
keystroke "w" using command down -- close the html file in Safari
keystroke "h" using command down -- hide Safari
end tell
end tell
-- delete the temp html file
do shell script "rm " & quoted form of POSIX path of tempFile
-- set the info in the mail message
tell application "Mail" to activate
tell application "System Events"
tell process "Mail"
set value of text field 1 of scroll area 2 of window 1 to receiver
set value of text field 1 of window 1 to theSubject
end tell
end tell