Use Speech to announce new Mail messages

Apr 23, '04 10:04:00AM

Contributed by: hotcocoa

I am very careful about who I give my main e-mail address out to, and as a result I literally get no junk mail. When I do get an e-mail, it's usually important and I should read it right away. So I wrote an AppleScript that does the following (and set it up as a rule in Mail so that occurs every time I get a new message ):

  1. Pauses iTunes (so I can hear what it has to say)
  2. Using Apple's Speech technology, it says "You have mail from: " and then it says out loud each person I received e-mail from
  3. Next it opens each e-mail message and then activates mail, so I can read the whole message

Script is as follows:

using terms from application "Mail"  
  -- This is what allows this AppleScript to run as a Rule  
  on run  
    --Performs the following action on each new mail received  
    tell application "Mail" to set sel to selection  
    tell me to perform mail action with messages (sel)  
  end run  
end using terms from  
  
using terms from application "Mail"  
  on perform mail action with messages selectedMsgs  
    -- See Mail's AppleScript dictionary for the full documentation on the  
    -- 'perform mail action with messages' handler.  
    tell application "iTunes"  
      --Pause iTunes to you can hear it what says next  
      pause  
    end tell  
    say "You have mail from"  
    set logString to "" & return  
    tell application "Mail"  
      set selCount to (count of selectedMsgs)  
      --A little bit of error handling  
      if selCount is equal to 0 then  
        set logString to logString & "There are no selected messages."  
      else if selCount is equal to 1 then  
        set logString to logString & "There is " & selCount ¬
        & " selected message."  
      else if selCount > 1 then  
        set logString to logString & "There are " & selCount ¬
        & " selected messages."  
      end if  
      repeat with counter from 1 to selCount  
        set msg to item counter of selectedMsgs  
        set theSubject to subject of msg  
        set theSender to sender of msg  
        set theSender to extract name from theSender  
        set logString to (logString & tab & "Message " & counter as string) ¬
        & " from: " & theSender & ", subject: " & theSubject & ".  "  
        if length of logString > 0 then  
          --note that you could have it speak the subject as well  
          say ":" & theSender  
        end if  
        --open the message in mail in a new window  
        open msg  
          
      end repeat  
    end tell  
    --open up mail to read the new messages  
    tell application "Mail"  
      activate  
    end tell  
      
  end perform mail action with messages  
end using terms from  
After saving the script somewhere, make sure you make a new Rule in Mail and set the conditions to match every message (or you could just have it run on select messages) and the action to run your newly saved script. I'm sure some AppleScript guru out there could improve and streamline my script, but it works for me!

Comments (29)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20040422104515106