AppleScript, Email Announcer
Here’s a little AppleScript I wrote to announce new emails in Mail.app. It pauses the iTunes if it’s playing, speaks the name of the sender if the message is not marked as junk and then resumes the playback.
on perform_mail_action(info)
-- see if iTunes is playing
set itunes_playing to false
tell application "System Events"
if (get name of every process) does not contain "iTunes" then
set itunes_playing to false
else
tell application "iTunes"
if player state is playing then set itunes_playing to true
end tell
end if
end tell
-- pause iTunes if needed
if itunes_playing is true then tell application "iTunes" to pause
tell application "Mail"
set the_messages to |SelectedMessages| of info
repeat with a_message in the_messages
set the_sender to extract name from sender of a_message
if (junk mail status of a_message) is not true then
say "Mail from " & the_sender
end if
end repeat
end tell
-- resume iTunes if it was playing before
if itunes_playing is true then tell application "iTunes" to play
end perform_mail_action
Keep in mind that the script might turn up to be quite chatty depending on the amount of mail that you receive every day. One way of dealing with this is to apply the script only to those emails sent by people in a certain group (Friends, Family, Colleagues and so on). Here’s one of my email rules:



This is a great script. i am in the process of sitching from Eudora to Mail and had a similar script for Eudora, Howvere, I;d like it even better if this cript would announce not the email part before the @ sign, but the name of the perosn which is often included before or after that email address.
For example: Jonathan Miller may email as jrm@xyz.com and rather than here “jrm” which probably sounds like “germ” I’d like ti to say “Jonathan Miller”
Can you help?
The line ’set the_sender to extract name from sender of a_message’ extracts the name of the sender from email header, not their email address. Unfortunately, for this to work correctly the field From: should include the name of the sender. It should look something like this:
From: Jonathan Miller <jrm@xyz.com>
If it doesn’t — that’s why you hear “germ”.
There’s couple of things you can do about it. First, and I understand that it’s not always possible, you can ask Jonathan to configure his email client to always put the name in the From: field. Most probably all it takes to achieve that is to fill in the name field of the account settings. Second, you can try and parse the signature from an email message, but I wouldn’t advice taking this route as it is not a simple task. Finally, you can create a LUT (look-up table) and manually populate it with custom names of your regular senders. This is the easiest and most reliable way of solving the problem. Please let me know if you need further help.
Is there a way to just get it to say “you got mail” or something to that effect rather than the senders name?
Mark, just replace this line:
say “Mail from ” & the_sender
with something like this:
say “You got mail.”
How do you get this to work? I can get into the Apple Script editor, but I have no clue where to go from there. Thanks!
Ok this is for Kayla: first copy the code above, then open Apple Script Editor, create a new document, paste the code, [DON'T CHANGE ANYTHING], save it as a script (Give it a name) and place it on ~/Library/Scrpts/Mail Scripts (If Mail Scripts doesn’t exist create it) btw “~” means the User name such as “John”, now open Mail, then mail preferences, and go to rules, create a new rule put any description you want like “Announce New Mail” then if ANY of the following conditions are met:, FROM, CONTAINS, then type “@” without the quotes, then where it says Perform the following actions: choose RUN APPLESCRIPT, then select the scripts from the folder where you placed it which is ~/Library/Scripts/Mail Scripts and Voilà! Test it with music playing on iTunes of course and ask someone to send you a e-mail message or just message yourself. IT SOUNDS BEAUTIFUL!!!
Hi,
The script is great. I just love it. However… I also use Mail to receive RSS articles. Whenever an RSS article arrives, the script pauses iTunes but doesn’t restart it. I’ve added an errorhandler and found that an error is generated (-1728 Can’t get <>). Obviously, there is no From: header in an RSS article. This causes the error. But, now I’m at a loss how to solve this.
Is there a way to detect the type of message (RSS or email) so I can distinguish between them, “say” different things and (most importantly) restart iTunes again?
Thanks, Mausy5043