Sunday, August 2, 2009

applescript for Mail.app growl notifications

Following up on my previous post, I also wanted similar growl notifications for Mail.app.

I wrote an applescript that will send a growl notification with the Sender and the Subject. The notification lives forever, until it is clicked. This is called 'sticky' in growl speak.

I did not attempt to normalize the body of the email to text, snip out the carriage returns and include the first 80 or so characters of the email body, in the notification. That would be cool(er).

Note that growl applescript interface does not support call backs... So, clicking on the notification can not take you to Mail.app or the message itself.

The script goes something like this:

-- Displays a growl notification, when invoked by Mail.apps rule engine
-- Author: rouble prmatta
to logit(log_string, log_file)
 do shell script ¬
  "echo `date '+%Y-%m-%d %T: '`\"" & log_string & ¬
  "\" >> $HOME/Library/Logs/" & log_file & ".log"
end logit

on run
 logit("in run()", "growl_notification")
 -- If there was some way to check if this app is already registered for 
 -- desired events to GrowlHelperApp, then we should do that here.
 -- I don't know of any way, just yet. No real harm in re-registering.
 register()
end run

using terms from application "Mail"
 on perform mail action with messages selectedMessages
  register()
  logit("performing mail action", "growl_notification")
  repeat with eachMessage in selectedMessages
   set theSubject to subject of eachMessage
   set theFrom to sender of eachMessage
   set theText to (content of eachMessage)
   set tid to AppleScript's text item delimiters
   
   set AppleScript's text item delimiters to " "
   try
    set theSummary to (text items 1 through 15 of theText) as text
   on error
    set theSummary to theText
   end try
   set AppleScript's text item delimiters to tid
   
   set theText to "From: " & theFrom & return & "Subject: " & theSubject & return & theSummary
   tell application "GrowlHelperApp"
    notify with name "Email Notification" title "You've got mail!" description theText application name "Mail.app Growl AppleScript" sticky yes
   end tell
  end repeat
 end perform mail action with messages
end using terms from

to register()
 logit("in register()", "growl_notification")
 tell application "GrowlHelperApp"
  -- Make a list of all the notification types that this script will ever send:
  set the allNotificationsList to {"Email Notification"}
  
  -- Make a list of the notifications that will be enabled by default.
  set the enabledNotificationsList to {"Email Notification"}
  
  -- Register our script with growl.
  register as application "Mail.app Growl AppleScript" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "Mail.app"
 end tell
end register

No comments:

Post a Comment