AppleScript is Easy.

Sike!

I’ve been re-reading AppleScript: The Definitive Guide this weekend. I’m fascinated by AppleScript. It’s unusual, quirky, english-like, and widely misunderstood. But most importantly, it’s somewhat poorly documented and therefore ripe for geek exploration and discovery.

Anyone with an interest in programming linguistics should take a little time to read up on AppleScript, IMO.

Here’s a fun little AppleScript I whipped up this evening… With the addition of a simple Rule in your Mail.app preferences, this script uses the Mac OS X speech functionality to give you a verbal alert whenever you receive a new message in your Mail.app Inbox.

Specifically, the script will speak the name and email address of the sender of the newly-received email message. I am constantly distracted at work by the red star that appears on the Mail.app Dock icon when I receive a new message… for some reason I feel compelled to immediately check my inbox to see who has just emailed me. With this script, your Mac will just tell you who’s just emailed while you continue to work (using a configurable voice and rate of speech). Much more convenient.

There are more possibilities with this script, as AppleScript has access to the Cocoa SpeechRecognitionServer which allows you to communicate with you Mac using verbal commands. Maybe I’ll take it further…

Here’s the script’s source code:


set theRate to 210
set theVoice to "Victoria"

tell application "Mail"
    set newMessages to (every message of inbox whose read status is false)
    set unreadCount to (count newMessages)
    if (unreadCount is greater than 0) then
        set theRate to "[[rate " & (theRate as string) & "]]"
        set spokenAlert to theRate
        set i to 0
        repeat while (i is less than unreadCount)
            set i to i + 1
            set aMessage to item i of newMessages
            set spokenAlert to spokenAlert & (the sender of aMessage as string)
            if (i is equal to unreadCount - 1) then
                set spokenAlert to spokenAlert & " and "
            else if (i is equal to unreadCount) then
                -- do nothing
            else if (i is less than unreadCount) then
                set spokenAlert to spokenAlert & ", "
            end if
        end repeat
        say theRate & "You are a fabulous, wonderful individual and you have new mail from " & spokenAlert using theVoice
    end if
end tell

To install:

  1. Copy the source above into ScriptEditor.app, and save it as a script or application to a convenient location on your local disk.
  2. Activate Mail.app and press command-, to view the preferences pane.
  3. Select the “Rules” pane and click “Add Rule”.
  4. In the first drop down box of the first rule condition, select Every Message.
  5. In the drop down for the first action to perform select Run AppleScript.
  6. Click the “Browse” button and select the path for the script you just downloaded.

About this entry