Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Need AppleScript to Reply to Email Message

I need an AppleScript to reply to messages I get from the commenting system (Disqus) on my blog. Here’s what it needs to do.


1. Reply to the message I have currently selected.

2. Delete everything in the body of the message.

3. Insert the word “Approve” in the body of the message.

4. Send the message.


That’s it. It seems like it should be easy, but I am not an AppleScript guru. I am hoping someone can share a quick snippet that will solve my problem.


Thanks.

MacBook Air, Mac OS X (10.7.2)

Posted on Oct 6, 2012 4:56 AM

Reply
Question marked as Best reply

Posted on Oct 6, 2012 8:52 AM

Hi,


The following script should do what you are asking for:


set myReply to "Approve"


tell application "Mail"

set theSelection to selection

if theSelection is {} then return

activate

repeat with thisMessage in theSelection

set theOutgoingMessage to replythisMessage with opening window

repeat until exists (window 1 whose name = "Re: " & subject of thisMessage)

end repeat

delay 0.1

tell application "System Events"

keystroke "a" using {command down} -- A to select all

keystrokemyReply

end tell

sendtheOutgoingMessage

end repeat

end tell


Message was edited by: Pierre L. (removed “tell process "Mail"”)

12 replies
Question marked as Best reply

Oct 6, 2012 8:52 AM in response to Michael Hyatt1

Hi,


The following script should do what you are asking for:


set myReply to "Approve"


tell application "Mail"

set theSelection to selection

if theSelection is {} then return

activate

repeat with thisMessage in theSelection

set theOutgoingMessage to replythisMessage with opening window

repeat until exists (window 1 whose name = "Re: " & subject of thisMessage)

end repeat

delay 0.1

tell application "System Events"

keystroke "a" using {command down} -- A to select all

keystrokemyReply

end tell

sendtheOutgoingMessage

end repeat

end tell


Message was edited by: Pierre L. (removed “tell process "Mail"”)

Oct 6, 2012 10:34 AM in response to Michael Hyatt1

I'm unable to reproduce that issue on my MacBook Pro under OS X 10.8.2. Try replacing “delay 0.1” with “delay 1” to begin with. If it doesn't work, open the Event Log History window to see if you couldn't find out some useful information as why the script doesn't work as expected.


Message was edited by: Pierre L. (try replacing “delay 0.1”, not “delay 0.25”)

Oct 6, 2012 11:47 AM in response to Pierre L.

I'm suspicious of the way you're nesting application tells. Try rewriting it this way:


set myReply to "Approve"


tell application "Mail"

set theSelection to selection

if theSelection is {} then return

activate

repeat with thisMessage in theSelection

set theOutgoingMessage to replythisMessage with opening window

repeat until exists (window 1 whose name = "Re: " & subject of thisMessage)

end repeat

delay 0.1


-- send gui scripting to handler to get it out of Mail tell block

my guiScriptMail(myReply)

sendtheOutgoingMessage

end repeat

end tell


on guiScriptMail(myReply)

tell application "System Events"

tell process "Mail"

set frontmost to true

keystroke "a" using {command down} -- A to select all

keystrokemyReply

end tell

end tell

end guiScriptMail

Oct 6, 2012 11:54 AM in response to Michael Hyatt1

Michael Hyatt1 wrote:


If you find an answer, please let me know. I am hoping it is not a bug with Mountain Lion.


Maybe you might want to try this alternative solution:


set myReply to "Approve"


tell application "Mail"

set theSelection to selection

if theSelection is {} then return

activate

repeat with thisMessage in theSelection

set theOutgoingMessage to replythisMessage with opening window

delay 1 -- adjust if necessary

if name of window 1 is not "Re: " & subject of thisMessage then

display dialog "There seems to be an issue with the current message." buttons {"Cancel", "Continue"} default button 2 with icon 2

else

tell application "System Events"

keystroke "a" using {command down} -- A to select all

keystrokemyReply

delay 0.25 -- enough time to keystroke

end tell

sendtheOutgoingMessage

end if

end repeat

end tell

Oct 6, 2012 12:05 PM in response to twtwtw

twtwtw wrote:


I'm suspicious of the way you're nesting application tells. Try rewriting it this way:


Thanks for your suggestion. However, since the original script seems to work flawlessly 99% of the time on my computer, I doubt it has anything do to with the way I'm nesting application tells. Maybe you go safer with a handler though.

Need AppleScript to Reply to Email Message

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.