Ringtones for Mail.app

by Robert Daeley

Last year, I worked out a method to create custom Mail.app announcement sounds by using the say program on Mac OS X, a CLI utility that converts text to speech and can output aiff sound files. And while nowadays I'm of the opinion that a totally hidden email program is a better way to go, it occurred to me earlier this week that it would be cool to have the equivalent of cellphone ringtones for Mail.

I wrote up a relatively simple AppleScript, which you can view below. You can also download ringtone.txt, which you will need to rename to 'ringtone.scpt' and open in the Script Editor application. (If you're unfamiliar with it, a description of Script Editor can be found within the article Hacking iPod and iTunes.)

At the top of the script are five options for you to set.

the_title is the exact title of the track you want to start playing. Likewise, the_artist is the exact name of the artist field.

the_playlist can be customized if you want, but it is set for the main Library and will most likely work fine for these purposes.

start_here allows you to select at what point in the song you want the ringtone to begin, in seconds; set at 0 (zero) to begin at the beginning. The stop_after property is how many seconds you want the song to play; set at 0 (zero) to disable and just keep on playing.

That's it for the AppleScript. Save the .scpt file wherever you like.

Within Mail, open the Preferences window and go to the Rules tab. Click Add Rule -- give it a Description, then choose whatever criteria you'd like -- most likely, it will be a From Contains username.

Under 'Perform the following actions', choose Run AppleScript from the popup menu, then find your ringtone.scpt via the Choose button. Hit OK and you're done.

And that's it! Now whenever an email from 'username' comes in, iTunes will switch on and let you know. Whether this is a good thing or not is, like cellphone ringtones, debatable ;) but it's a good exercise on getting Mac GUI apps interacting together.

To set up separate ringtones for different people, just duplicate the ringtone.scpt file, one for each, and customize the properties as desired. Then set up a Mail Rule for each as above.

property the_title : "Gouge Away"
property the_artist : "Pixies"
property the_playlist : "Library"
property start_here : 23 -- set to 0 to begin at the beginning
property stop_after : 0 -- set to 0 to disable
tell application "iTunes"
set the matches to (every track of playlist the_playlist whose name is the_title) as list
if the (count of matches) > 0 then
if the (count of matches) = 1 then
set the_track to item 1 of matches
repeat with t from 1 to the (count of matches)
if the artist of item t of matches is the_artist then
set the_track to item t of matches
exit repeat
end if
end repeat
end if
play the_track
set player position to start_here
if stop_after > 0 then
delay stop_after + 1
end if
display dialog "No song matches. Please confirm details." buttons {"Yikes!"} default button 1
end if
end tell

I should probably put in a disclaimer that this worked on my combination of versions: 10.4.3, Mail 2.0.5, and iTunes 6.0.1, and thus may not work if yours differ, or if you're not a Capricorn, or if Venus is in retrograde.

(Thanks to Doug's AppleScripts for iTunes for some direction.)


2005-11-30 05:40:22
A little bug?
Thanks, this is a great little script. Only on a Max.

I got an iTunes scripting component error at "play the_track". I have iTunes 6.0.1 and osx 10.4.3. Making it just "play" makes it, well, just play.

2005-11-30 09:12:29
A little bug?
Hmm -- just on a hunch, does the error happen if the stop_after property is set to "0"?
Andy Guice
2006-05-08 15:14:25
Thanks for the script, its great! Was wondering if there is anything you can add to the script to play the song that was playing anyway afterwards. For instance, I am listening to an Album on iTunes, and then I get an email, the appescript plays the 'ringtone' and then returns me to the same song and spot at which I was in the song.