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
  else
   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
   pause
  end if
 else
  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.)