Growl Notification from Mail.app with Sender Photos

Feb 09, '12 07:30:00AM

Contributed by: wassimj

You can use an AppleScript to send a notification to Growl whenever new messages arrive in Mail.app. The script also includes the image of the sender from your Address Book as the icon of the Growl notification. Works in Snow Leopard and Lion.

I adapted a script written originally by Hunter Ford. I added the ability to include the image of the sender from the Address Book (if it exists). Britney F. helped me solve a couple of bugs -- Many thanks!

First you have to be using Mail.app and the latest version of Growl.

You can download the script from here and place in your /Library/Scripts folder or anywhere you wish.

To use this script, you have to add a rule in Mail.app to run the script for all new messages.

In Mail.app choose Preferences » Rules » Add New Rule. Then set the condition to 'Every Message' and the action to 'Run AppleScript' then choose the Mail2Growl.scpt from the folder you saved it in.

Whenever you get a new message, a Growl notification will appear with the image of the sender or a placeholder image if you do not have his/her image in your Address Book.

Here's the script (save it as Mail2Growl.scpt):

-- Mail2Growl
-- Growl Alerts in Mail
-- Wassim Jabi [http://wjabi.net]
-- Modified from the excellent script by Hunter Ford [http://www.cupcakewithsprinkles.com]
-- If it exists, this script sends the photo of the sender to Growl from the address book. Otherwise, it sends the sender place holder image from Mail.app.
-- Original script found at [http://hunterford.me/growl-notifications-for-apple-mail-on-mac-os-x/]
-- This script uses a tip to summarise many messages by "Ryan" in the comments section at [http://hunterford.me/growl-notifications-for-apple-mail-on-mac-os-x/]
-- This script also used tips from [http://www.macosxtips.co.uk] for finding photos in the Address Book
-- This script arises from the lack of any Growl Support in Mac OS X Snow Leopard (10.6) or Lion (10.7)
-- Code inspired by and adapted from James Higgs [http://blog.jameshiggs.com/2009/08/28/growlmail-on-snow-leopard-a-temporary-fix/] as well as those mentioned.

tell application "GrowlHelperApp"
  -- Make a list of all the notification types
  -- that this script will ever send:
  set the allNotificationsList to {"New Email"}
  
  -- Make a list of the notifications
  -- that will be enabled by default.
  -- Those not enabled by default can be enabled later
  -- in the 'Applications' tab of the growl prefpane.
  set the enabledNotificationsList to {"New Email"}
  
  -- Register our script with growl.
  -- You can optionally (as here) set a default icon
  -- for this script's notifications.
  register as application "Mail" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "Mail"
end tell

-- Mail Rule Trigger
--
-- Source: Benjamin S. Waldie [http://www.mactech.com/articles/mactech/Vol.21/21.09/ScriptingMail/index.html]
using terms from application "Mail"
  on perform mail action with messages theSelectedMessages for rule theRule
    set N to count items of theSelectedMessages
    if (N > 5) then
      set multipleDescription to "" & N & " new messages"
      tell application "GrowlHelperApp"
        notify with name "New Email" title "You have " & N & " new messages." description " " application name "Mail"
      end tell
    else
      repeat with thisMessage in theSelectedMessages
        -- Process the current message
        
        -- Grab the subject and sender of the message
        set growlSender to my ExtractName(sender of thisMessage)
        try
          set growlPhoto to my getPhotoFromAddressBook(my ExtractEmailAddress(sender of thisMessage))
        on error
          set growlPhoto to read POSIX file "/Applications/Mail.app/Contents/Resources/SenderImagePlaceholder.png" as "TIFF"
        end try
        set growlSubject to subject of thisMessage
        set growlTitle to growlSender & return & growlSubject
        -- Use the first 100 characters of a message
        set growlDescription to (content of thisMessage)
        set growlLength to (length of growlDescription)
        
        if growlLength > 100 then
          set growlDescription to "" & (characters 1 through 100 of growlDescription) & " …"
        end if
        
        -- Send a Notification
        tell application "GrowlHelperApp"
          try
            notify with name "New Email" title growlTitle description growlDescription application name "Mail" image growlPhoto
          on error
            set growlPhoto to (read POSIX file "/Applications/Mail.app/Contents/Resources/SenderImagePlaceholder.png" as "TIFF")
            notify with name "New Email" title growlTitle description growlDescription application name "Mail" image growlPhoto
          end try
        end tell
      end repeat
    end if
  end perform mail action with messages
end using terms from

-- *ExtractName*
--
-- gathers the name portion from the "From: " line
--
-- Source: robJ [http://forums.macosxhints.com/archive/index.php/t-19954.html]
to ExtractName(sender_)
  if sender_ begins with "<" then
    return text 2 thru -2 of sender_
  else
    set oldTIDs to text item delimiters
    try
      set text item delimiters to "<"
      set name_ to first text item of sender_
      set text item delimiters to oldTIDs
    on error
      set text item delimiters to oldTIDs
    end try
    return name_ as string
  end if
end ExtractName

-- *ExtractEmailAddress*
--
-- gathers the email address portion from the "From: " line
--
-- Source: robJ [http://forums.macosxhints.com/archive/index.php/t-19954.html]
to ExtractEmailAddress(sender_)
  if sender_ begins with "<" then
    return text 2 thru -2 of sender_
  else
    set oldTIDs to text item delimiters
    try
      set text item delimiters to {"<", ">"}
      set email_ to second text item of sender_
      set text item delimiters to oldTIDs
    on error
      set text item delimiters to oldTIDs
    end try
    return email_ as string
  end if
end ExtractEmailAddress


to getPhotoFromAddressBook(sender_)
  set photo_ to read POSIX file "/Applications/Mail.app/Contents/Resources/SenderImagePlaceholder.png" as "TIFF"
  try
    tell application "Address Book"
      set photo_ to image of people whose value of emails contains sender_
    end tell
  on error
    set photo_ to read POSIX file "/Applications/Mail.app/Contents/Resources/SenderImagePlaceholder.png" as "TIFF"
  end try
  return photo_
end getPhotoFromAddressBook

[crarko adds: Tested briefly, seemed to work OK in 10.6.8. It looks like the most recent version of Growl (for Lion) is in the Mac App Store for US $1.99. The option to build it from source is still available as well.]

Comments (7)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20120122112518600