I am trying to have Mail send an email in formatted text via Applescript.
I found the text below (in a larger script) in another forum. But it does not format the text my email at all.
And I do not understand as to how to fix it. Any suggestions?
Thanks
set newMessage to make new outgoing message with properties {subject:the_subject, content:the_content}
tell newMessage
set x to offset of "Universal Widgets Inc.." in the_content
set font of characters x thru (x + (count of "Universal Widgets Inc..")) of content to "Helvetica Bold"
set color of characters x thru (x + (count of "Universal Widgets Inc..")) of content to {56342, 2442, 607}
set x to offset of TheName in the_content
set font of characters x thru (x + (count of TheName)) of content to "Helvetica Bold"
set x to offset of PrintDateTimeName in the_content
set font of characters x thru (x + (count of PrintDateTimeName)) of content to "Helvetica Bold"
set x to offset of "'Unrecognized items for Manual Printing'." in the_content
set font of characters x thru (x + (count of "'Unrecognized items for Manual Printing'.")) of content to "Helvetica Bold"
I have better luck when I apply styles to paragraphs (if this helps at all)
So, to make the first line of this post bold, you set properties of paragraph 1 etc.
tell application "Mail"
activate
set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
tell theMessage
make new to recipient with properties {name:recipientName, address:recipientAddress}
set properties of every paragraph to {font:"Lucida Grande", size:"12"}
set properties of paragraph 1 to {font:"Helvetica-Bold"}
set properties of paragraph 5 to {font:"Helvetica-Bold"}
set properties of paragraph 10 to {font:"Helvetica-Bold"}
set properties of paragraph 20 to {font:"Helvetica-Bold"}
end tell
end tell
I had to put in a delay to make the formatting happen.
tell application "Mail"
activate
set theMessage to make new outgoing message with properties {subject:thesubject, content:thecontent, visible:true}
tell theMessage
make new to recipient with properties {name:TheName, address:themailto}
set properties of every paragraph to {font:"Lucida Grande", size:"12"}
set properties of paragraph 1 to {font:"Helvetica-Bold"}
set properties of paragraph 5 to {font:"Helvetica-Bold"}
set properties of paragraph 10 to {font:"Helvetica-Bold"}
delay 3
send
end tell
end tell
One more question, could I underline a word? and if so how?
Thanks for the script. But why I can’t make italic paragraph?
set recipientAddress to "KNIAZIDIS.ROMPERT@gmail.com"
set theSubject to "This is the subject"
set recipientName to "Robert Kniazidis"
set theContent to "This is a paragraph.
This is a bold paragraph.
This is italic paragraph."
tell application "Mail"
activate
set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
tell theMessage
make new to recipient with properties {name:recipientName, address:recipientAddress}
set properties of paragraph 1 to {font:"Helvetica", size:12.0, color:{0, 65535, 65535}}
set properties of paragraph 2 to {font:"Helvetica-Bold", size:25.0, color:{65535, 0, 65535}}
set properties of paragraph 3 to {font:"Helvetica-Italic", size:18.0, color:{65535, 65535, 0}}
delay 3
send
end tell
end tell
Update: Solved:
set properties of paragraph 3 to {font:"Helvetica-Oblique", size:18.0, color:{65535, 65535, 0}}
tell application "Mail"
set newMessage to make new outgoing message with properties {subject:the_subject, content:the_content}
tell newMessage
set x to offset of "Universal Widgets Inc.." in the_content
set font of characters x thru (x + (count of "Universal Widgets Inc..")) to "Helvetica Bold"
set color of characters x thru (x + (count of "Universal Widgets Inc..")) to {56342, 2442, 607}
set x to offset of TheName in the_content
set font of characters x thru (x + (count of TheName)) to "Helvetica Bold"
set x to offset of PrintDateTimeName in the_content
set font of characters x thru (x + (count of PrintDateTimeName)) to "Helvetica Bold"
set x to offset of "'Unrecognized items for Manual Printing'." in the_content
set font of characters x thru (x + (count of "'Unrecognized items for Manual Printing'.")) to "Helvetica Bold"
end tell
end tell
I tested with this script. It works fine:
set recipientAddress to "KNIAZIDIS.ROMPERT@gmail.com"
set the_subject to "This is the subject"
set recipientName to "Robert Kniazidis"
set theName to "myName"
set PrintDateTimeName to "date"
set the_content to "This is \"Universal Widgets Inc...\"
This is a myName paragraph.
This is date paragraph."
tell application "Mail"
set newMessage to make new outgoing message with properties {subject:the_subject, content:the_content}
tell newMessage
make new to recipient with properties {name:recipientName, address:recipientAddress}
set properties of every paragraph to {font:"Lucida Grande", size:"24", color:{0, 0, 0}}
set x to offset of "Universal Widgets Inc.." in the_content
set font of characters x thru (x + (count of "Universal Widgets Inc..")) to "Helvetica Bold"
set color of characters x thru (x + (count of "Universal Widgets Inc..")) to {56342, 2442, 607}
set x to offset of theName in the_content
set font of characters x thru (x + (count of theName)) to "Helvetica Bold"
set x to offset of PrintDateTimeName in the_content
set font of characters x thru (x + (count of PrintDateTimeName)) to "Helvetica Bold"
set x to offset of "'Unrecognized items for Manual Printing'." in the_content
set font of characters x thru (x + (count of "'Unrecognized items for Manual Printing'.")) to "Helvetica Bold"
delay 3
send
end tell
end tell
And… 1) It is only for new outgoing messages 2) you should set the font of character to “Helvetica-Oblique” and not set the character itself to “Helvetica-Oblique”.
May be other issue too: not all characters have nonregular typefaces.
The Typefaces you can add to name of each font in “Mail” programatically is 6:
“Regular”, “Oblique”, “Light”, “Light Oblique”, “Bold”, “Bold Oblique”. I don’t see any “Underlined”, so I think, this will be done only with GUI scripting.
Here, I set each word “paragraph” of message’s content to yellow color and italic font:
set recipientAddress to "KNIAZIDIS.ROMPERT@gmail.com"
set the_subject to "This is the subject"
set recipientName to "Robert Kniazidis"
set the_content to "This is \"Universal Widgets Inc...\"
This is a myName paragraph.
This is date paragraph."
tell application "Mail"
activate
set newMessage to make new outgoing message with properties {subject:the_subject, content:the_content}
tell newMessage
make new to recipient with properties {name:recipientName, address:recipientAddress}
repeat with i from 1 to (count of words)
set theVALUE to word i
set theWord to (a reference to word i)
if theVALUE = "paragraph" then set properties of theWord to {size:12.0, font:"Helvetica-Oblique", color:{65355, 65355, 0}}
end repeat
delay 3
send
end tell
end tell
This is a workaround to be able to add attributes which vanilla AppleScript doesn’t support.
The script creates an NSAttributedString with AppleScriptObjC, the string is copied to the clipboard as RTF and then pasted into the mail body with GUI scripting.
use AppleScript version "2.5"
use framework "Foundation"
use framework "AppKit"
use scripting additions
property |⌘| : a reference to current application
set theString to "This is an attributed string"
set attributes to |⌘|'s NSMutableDictionary's dictionaryWithObjects:{|⌘|'s NSFont's systemFontOfSize:18} forKeys:{|⌘|'s NSFontAttributeName}
set attributedString to |⌘|'s NSMutableAttributedString's alloc()'s initWithString:theString attributes:attributes
set isRange to attributedString's |string|()'s rangeOfString:" is " options:0
attributedString's addAttribute:(|⌘|'s NSBaselineOffsetAttributeName) value:10 range:isRange
set attributedRange to attributedString's |string|()'s rangeOfString:"attributed" options:0
attributedString's addAttribute:(|⌘|'s NSUnderlineStyleAttributeName) value:1 range:attributedRange
set rtfData to attributedString's RTFFromRange:{0, attributedString's |length|()} documentAttributes:{DocumentType:|⌘|'s NSRTFTextDocumentType}
set pasteb to |⌘|'s NSPasteboard's generalPasteboard()
pasteb's clearContents()
pasteb's setData:rtfData forType:(|⌘|'s NSPasteboardTypeRTF)
tell application "Mail"
activate
set newMessage to make new outgoing message with properties {subject:"Hello World", visible:true}
tell newMessage
make new to recipient with properties {name:"John Doe", address:"john@doe.com"}
end tell
end tell
tell application "System Events"
tell process "Mail"
set focused of UI element 1 of scroll area 1 of window 1 to true
keystroke "v" using command down
keystroke return
end tell
end tell