Getting a notification for Unix mail through Growl via Applescript

This installation lets you get a notification through growl whenever a new unix mail arrives.
Might be of use to those of you who ever have been annoyed by the fact that you actually have
to go in and check for mail, or forgot to do so about a process.

By the way I use to supress the initial mail from cron by typing “1&2 >/dev/null”
after the command on the commandline in Cronnix because I find it of litte interest
most of the time as to whence a job was run.

How it works:
A launchdAgent installed by you checks if your mailfile has changed and UnixMailNotifier is
called when that is true. The UnixMailNotifier reacts intelligently upon which requests it
should display a notification for, as your mailfile also will be updated when you quit mail.

INSTALLATION

Prerequisites:
You need to have installed Growl and growl helper.
You need to either make or provide a folder for which the script can be run from.

  1. Installing the property list file

    Copy the plist file below and save it into ~/Library/LaunchAgents as com.MacUser06.plist
    Editing

    Replacethe entry that spells YourUserName,with your username (login name)
    The entry YourHomeFolder with the full path to your homefolder.
    The entry YourWorkingFolder with the path relative to your homefolder.

<?xml version="1.0" encoding="UTF-8"?> Label com.MacUser06.UnixMailNotifier Program /YourHomefolder/YourWorkingFolder/UnixMailNotifier.sh ProgramArguments /YourHomefolder/YourWorkingFolder/UnixMailNotifier.sh RunAtLoad KeepAlive SuccessfulExit WatchPaths /var/mail/YourUserName
  1. Installing the notifier script.
    Copy the script below and save it into the scripts working folder as UnixMailNotifier.sh
    Replace the text FullPathToYourWorkingFolder with the full path to your working folder.
    Save it.

You must also set its executable bit with the command chmod u+x

----------------------UnixMailNotifier.sh
#!/bin/bash
mail -H >|/FullPathToYourWorkingFolder/out 2>/dev/null
# if this file is empty then we know for sure that there is no mail.
export outcnt=cat /FullPathToYourWorkingFolder/out
if test $outcnt = “” ; then
exit 0
fi
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

  1. Launch the com.MacUser06.UnixMailNotifier.plist for the first and only time.

    Enter ~/Library/LaunchAgents
    launchctl load com.MacUser06.UnixMailNotifier.plist

  • From now on you should be notified when ever you get a new mail, except when logging in.
  1. Add the UnixMailNotifierScript to your login items in the Accounts preference pane, so that
    you will get a notification if there has happened stuff while you were logged out.
    This is mainly of interest if you get forwarded mail from your root acount.

Enjoy!

McUsr