Unix mail notifications via Growl

Jun 14, '11 07:30:00AM

Contributed by: MacUser06

This tip lets you get a notification through Growl whenever new Unix mail arrives, with the subject of the mail. I get Unix mail as a result of run cron-jobs. I use cron because whether you have lingon or not; it is far easier to schedule a job with cron.

When there is something that I really want to watch from a process run by cron I do:

echo "the very interesting stuff" |mail -s "IMPORTANT" myusername

or some other Unix command to send the same mail message.

The problem is that I have to manually go into Unix mail and manually check if there is any new mail or not, and sometimes I just forget to do that.

Therefor I have made a notification in Growl via a launchd job and an AppleScript.

Prerequisites: You need to have installed Growl and Growl helper.

The lanchd process checks if your /var/mail/You mail file has changed, and if it has, the launchd process will run the script below and die until next time a new mail arrives and changes the mailfile. The AppleScript makes Growl show that there is a new mail with subject line of the last arrived mail in the bezel window.

Installation:

This script creates a Growl notification and must be placed somewhere on your disk. I have folder named .UserAgents for that purpose; to keep them collected. And a folder named .Processess for the jobs I run through cron via Cronnix.

The script must be installed via a property list file looking like the one given after the shell script below, except that you have to adjust it to insert the correct path for both the shell script and your mailfile.

You must also set its executable bit with the command chmod u+x from a Terminal window.

--------UnixMailNotifier.sh
#!/bin/bash
export mvar=`mail -H |tail -1 |sed -n 's/(..*")([^"][^"].*)(["].*)/2/p'`
/usr/bin/osascript <<-EOF 2>/dev/null 1>&2
  tell application "GrowlHelperApp"
    try
      set the allNotificationsList to ¬
        {"Unix Mail Notification"}
      set the enabledNotificationsList to ¬
        {"Unix Mail Notification"}
      register as application ¬
        "Growls Unix Mail Notification" all notifications allNotificationsList ¬
        default notifications enabledNotificationsList ¬
        icon of application "Terminal.app"
      
      --  Send a Notification...
      notify with name ¬
        "Unix Mail Notification" title ¬
        "Unix Mail Notification" description ¬
        "Subject: $mvar" application name "Growls Unix Mail Notification" icon of application "Terminal.app"
      
    on error the error_message number the error_number
      display dialog "Error: " & the error_number & ". " & the error_message buttons {"Cancel"} default button 1
    end try
    
  end tell
EOF
The property list file which must be installed in ~/Library/LaunchAgents looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>com.You.UnixMailNotifier</string>
  <key>Program</key>
  <string>/Users/You/.UserAgents/UnixMailNotifier.sh</string>
  <key>ProgramArguments</key>
  <array>
    <string>/Users/You/.UserAgents/UnixMailNotifier.sh</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>KeepAlive</key>
  <dict>
    <key>SuccessfulExit</key>
    <false/>
  </dict>
  <key>WatchPaths</key>
  <array>
    <string>/var/mail/You</string>
  </array>
</dict>
</plist>
After you have saved the property list file as (for instance) com.You.UnixMailNotification.plist you must start it with:

launchctl load com.you.UnixMailNotification.plist.

From there on you should get a notification whenever you get a Unix mail.

[crarko adds: I haven't tested this one. If there are any errors in either the script or the plist please let me know via the comments and I will correct them.]

Comments (7)


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