Replying to email messages

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’m running Mojave.

Mark,

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.

Ray

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