Last modified on 13 July 2013, at 11:54

AppleScript Programming/Mail alert

Note: the following will bring your volume low after raising it with the alert.

From anyone (at "Tools->Rules", create a new rule, add criterion such as "Is not junk email", and for an action, choose "Run AppleScript", selecting the script file for the script below):

tell application "Mail"
	set volume 10
	say "You've got mail!"
	set volume 1
end tell

The code above does work, yet there are a couple of technical improvements that could be made to it.

We don't need to tell Finder to set the volume and to say "You've got mail!". These commands are built into AppleScript, so we could and should do the following instead:

set volume 10
say "You've got mail!"
set volume 1

There's another improvement that needs to be made and that is the script shouldn't change the volume from what it was to one. In general code shouldn't have "side-effects" like this. Things should be restored to what they were before the script was called:

set theOldVolume to output volume of (get volume settings)
set volume 10
say "You've got mail!"
set volume theOldVolume

Mail can be configured to call different scripts, depending upon different conditions such as who the email is from. If you wanted your Mac to say "You've got mail from Bob!" when receiving and email from Bob, you could define the following script and install it in Mail's scripts folder and define an email rule to call this script when receiving email from Bob.

set theOldVolume to output volume of (get volume settings)
set volume 10
say "You've got mail from Bob!"
set volume theOldVolume