« Epic hailstorm or neighborhood animal roof party? | Main | Keyboard shortcuts for AppleScripts in Snow Leopard? »

AppleScript autoresponder in Snow Leopard's Mail.app

| 2 Comments

Last year, I tried to create an autoresponder for Apple's Mail.app, but ran into problems. Now, in Snow Leopard, I've finally got something working that suits my needs. I'm posting about it because people contact me now and then asking for help with this, and I haven't been able to help 'til now. However, my approach here may not suit everyone's needs.

To recap my history with this:

Mail.app allows you to create a rule that sends an autoresponse to an incoming message. However, the autoresponse automatically includes the full text of the original message, and all attachments. I don't know of a way to turn that off.

Also, the autoresponse uses rich text rather than plain text, and again I don't know of a way to turn that off.

There's a free autoresponder script that uses AppleScript to implement an autoresponder that doesn't include the original message. However, in the Leopard version of Mail.app, that script didn't work, because (as noted in a Mac OS X Hints article) the AppleScript command to create a new mail message in Leopard didn't work when called from a rule.

I ended up purchasing a Mail.app plugin called MailTemplate. It couldn't be called from a Rule, so I had to send "auto"responses by hand, but otherwise it did pretty much what I wanted. So that's what I've been using for autoresponses for over a year.

But yesterday, I installed Snow Leopard—and then I discovered that MailTemplate doesn't yet work with Snow Leopard.

So I went back to the abovelinked about.com AppleScript—and found that it now works! Mail.app in Snow Leopard apparently no longer has the bug that prevented message creation from AppleScript invoked by a rule.

So for most people who want an autoresponder, using Mail.app in Snow Leopard should let you use that script. (If you don't mind sending rich-text email.)

But I haven't tested that approach extensively because in the past year I've come to like having manual control over the autoresponses.

I may go back to truly automatic responses soon, now that I can. But in the meantime, I've put together a script that I can call manually.

The idea is that I manually select the messages I want to reply to, and then I invoke the script. (I hope to assign a keyboard shortcut to it, but haven't yet gotten that part to work.) For each of my selected incoming messages, the script creates a response message, displays it on the screen, and changes it to plain text. When the script finishes, I can click the Send button for each of the generated responses.

It's perhaps not quite as easy to use or as powerful as MailTemplate, but it seems to work pretty well.

Here's the script:

using terms from application "Mail"
  on run
    tell application "Mail"
      set theMessages to selection
      repeat with eachMessage in theMessages
        set theName to extract name from sender of eachMessage
        if exists reply to of eachMessage then
          set theAddress to reply to of eachMessage
        else
          set theAddress to extract address from sender of eachMessage
        end if
        set newMessage to make new outgoing message
        tell newMessage
          make new to recipient at end of to recipients with properties {name:theName, address:theAddress}
          set visible to true # Show the draft message in a window
          set subject to "Re: " & subject of eachMessage
          set sender to "[Your Name] <[Your email address]>"
          set content to "Dear " & theName & ",

[Autoresponse text goes here. Be sure to put a backslash before each double-quotation mark.]
"
        end tell
        # Now change the content to plain text:
        tell application "System Events"
          tell application process "Mail"
            set frontmost to true
          end tell
          keystroke "T" using {command down, shift down}
        end tell
        # If you want to send the message immediately,
        # then uncomment the following line:
        #send newMessage
        tell eachMessage
          set was replied to to true
        end tell
      end repeat
    end tell
  end run
end using terms from

I stole various parts of this from various places, but most of it comes from that about.com script.

The way I'm changing the content to plain text is a kludge. I tried various things, including the following line:

set content to «class ktxt» of ((content as string) as record)

But still the messages I was sending were plain text followed by rich text. So I ended up using the keystroke command to simulate pressing Command+Shift+T, the Make Plain Text command's keyboard shortcut. That code probably isn't very robust; for example, I'm not sure what would happen if you brought another window to the front while the script is running.

Note that the above version of the script just creates the messages; it doesn't send them. You have to click the Send button for each generated message. To instead have the script automatically send the messages, uncomment the send newMessage line.

2 Comments

Hello Jed,
I thank you for the post because I am about to write a script myself and I needed an example (I never wrote an Apple script before).
Actually my script is about a very simple thing that is almost impossible to get on any mail client (an on gmail) : trigger rules AFTER READING an email.
May sound strange to some people but I have over 100 folders and the idea of browsing all of them to find my incoming mail is just not what I need. However it would be nice to have the mail moved to the appropriate folder after I read it, instead of having those 2000+ mails (I know it is a shame) in my Inbox.
I ignored how powerful Apple script seems to be, so I will give it a try. And may be I let you know how it went.
Thanks again.


hi

Do you have any good suggestions for me wanting to learn apple script?

I'm learning "C" and think that Apple Script will be eassier to learn.

If you use twitter, catch me on @creepyblindy

That's the best way to get ahold of me.

Mike


Post a comment