Growl, Mail.app and AppleScript

Posted by Kevin Way Fri, 17 Aug 2007 18:55:00 GMT

I wanted a simple system that would notify me whenever I received an e-mail from somebody “important”, but not for every new message that hit my inbox.

I tried GrowlMail and Mail.Appetizer, but found them both unsatisfactory. I finally decided that what I really wanted was a growl message to appear for messages from selected recipients. Something with the name and the subject of the e-mail, like this:

Fortunately, it turned out to be reasonably simple.

First, I opened up Address Book, and made sure that the important people were all listed in appropriate groups and correct e-mail addresses.

Having done that, I opened Mail.app, and added a rule:

Description: Growl

If any of the following conditions are met:
Sender is member of Group: Clients
Sender is member of Group: Family
Sender is member of Group: InsideSystems

Perform the following actions:
Run AppleScript: ~/Library/Scripts/Applications/Mail/MailScript.scpt
but ho, you say, that script does not exist! Never fear, then I loaded up Script Editor and created MailScript.scpt as follows:

on perform_mail_action(info)

    tell application "Mail" 

        set selectedMessages to |SelectedMessages| of info
        set theRule to |Rule| of info

        repeat with eachMessage in selectedMessages
            set theSubject to subject of eachMessage
            set theSender to sender of eachMessage

            tell application "GrowlHelperApp" 

                set the allNotificationsList to ¬
                    {"New Mail"}

                set the enabledNotificationsList to ¬
                    {"New Mail"}

                register as application ¬
                    "MailScript" all notifications allNotificationsList ¬
                    default notifications enabledNotificationsList ¬
                    icon of application "Mail" 

                notify with name ¬
                    "New Mail" title ¬
                    "New Mail from " & theSender description ¬
                    theSubject application name "MailScript" 

            end tell

        end repeat
    end tell
end perform_mail_action

Now, this was the first time I ever did anything in AppleScript, so it’s quite possible there is a much better way to do all of this. As it stands, I simply glued together some sample code from Growl and some sample code from this hint on the excellent MacOSXHints.com, and it appears to work as desired.

Finally, I went into the growl prefpane, and set the priority and stickiness, so that these messages would stay on my screen even if I happened to be otherwise occupied when they came in.

Voila! Nothing spectacularly complex, but it’s a nice example of what I like about Apple. Everything’s easy to use out of the box, but when I dreamed up a random feature, I was able to implement it in a short period of time, despite having never even used their glue language before.

Hopefully somebody else would like this feature too!

Comments

Leave a response

Comments