I’m trying to write a script that replies to the currently selected email message. I can create the reply email easily enough with Mail’s reply command, but I cannot seem to alter the content of the message.
Here’s what I have:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "Mail"
set firstSelectedMessage to item 1 of (get selection) -- get the currently selected email
set newMessage to reply firstSelectedMessage with opening window -- create a reply message
-- Alter the reply message...
set existingContent to newMessage's content
set newContent to "Hello,
Some additional text goes here...
" & existingContent
set newMessage's content to newContent
-- Check that the change took
set xxx to newMessage's content
if xxx = newContent then
display alert "Good!"
else if xxx = existingContent then
display alert "Nothing's Changed"
else
display alert "Bad"
end if
end tell
When I run this, I get the “Nothing’s Changed” alert. I cannot seem to get my changes to the reply message’s content to be accepted. Does anyone have any insight into this?
I’ve only been able to do this reliably by creating a new outgoing message and setting the address, subject and content as if it was a reply. It’s an old problem—not a new issue in Mojave.
Perhaps others will have a better route, but for me trying to make Mail work the way the dictionary looks like it should work is a path to madness. I’ve spent too much time watching some needed properties appear to be there in Explorer, but then disappearing after a fraction of a second.
Thanks Ray, I’ll stop trying to use the reply event.
A working script looks like this:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "Mail"
set firstSelectedMessage to item 1 of (get selection) -- get the currently selected email
set msgSubject to firstSelectedMessage's subject
set msgSender to firstSelectedMessage's sender
set msgContent to firstSelectedMessage's content
set newSubject to "Re: " & msgSubject
set newContent to "Hello,
Some additional text goes here...
" & msgContent
set newMessage to make new outgoing message with properties {subject:newSubject, content:newContent}
tell newMessage
make new to recipient at end of to recipients with properties {address:msgSender}
--send
end tell
end tell
For anybody finding this post through Google, nb that (at least on Ventura / 13.3) it is possible to add text to a reply message by creating a signature containing the text you want and then using that:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
set replyText to "Thank you for your message, which I will consider carefully."
tell application "Mail"
make new signature with properties {name:"tempSignature", content:replyText}
set selectedMessages to selection
if selectedMessages is not {} then
set theMessage to item 1 of selectedMessages
set theReply to reply theMessage with opening window
set theReply's message signature to signature "tempSignature"
activate
end if
delete signature "tempSignature"
end tell
This leaves an empty line at the top of the message, though.