Search
Photos
Admin
« AppleScript, "Open Safe Files" Folder Action | Main | Refactoring »
Thursday
Oct272005

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 performmailaction(info)
-- see if iTunes is playing
set itunesplaying 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 itunesplaying 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 themessages to |SelectedMessages| of info

repeat with a
message in themessages

set the
sender to extract name from sender of amessage

if (junk mail status of a
message) is not true then

say "Mail from " & thesender

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 performmailaction



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:



Email From Family Rule


Reader Comments (17)

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?

February 28, 2006 | Unregistered CommenterIrene

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.

March 2, 2006 | Unregistered CommenterGeorge Sudarkoff

Is there a way to just get it to say "you got mail" or something to that effect rather than the senders name?

December 9, 2008 | Unregistered CommenterMark

Mark, just replace this line:
say "Mail from " & the_sender
with something like this:
say "You got mail."

December 9, 2008 | Unregistered CommenterGeorge Sudarkoff

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!

January 18, 2009 | Unregistered CommenterKayla

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!!!

June 1, 2009 | Unregistered CommenterMarco

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

July 5, 2009 | Unregistered CommenterMausy5043

Hi - I love the concept of this script however I am having a problem. I am using SL with Mail.app 4.2. The script announces the name of the previous email vs the latest email that came in. Any ideas?

November 12, 2009 | Unregistered Commenterryan

Awesome script! Thanks so much for sharing this.

November 16, 2009 | Unregistered CommenterGreg Batla

Love it also. But, I have the same problem with Mail 4.2.

December 1, 2009 | Unregistered CommenterDan

this is a great script! works fine too...
i was wondering who i can make it say:

You have an email from [sender] with the subject [subject]

i tried
say "You have an email from " & the_sender & "with the subject " & the_subject

this doesn't work. i take it that i've made a mistake with the "the_subject" part. can anyone help?

thanks!

December 1, 2009 | Unregistered Commentersami

oh i just got it...
just had to add the following line:
set the_subject to extract name from subject of a_message

December 1, 2009 | Unregistered Commentersami

[...] всего скачайте и сохраните сценарий. Потом в настройках Mail создайте [...]

[...] de música en iTunes que tengamos en marcha en ese momento. Para empezar vamos a la web de George Sudarkoff y copiamos el texto del script que ha diseñado para anunciar la llegada de emails. En nuestro Mac [...]

December 7, 2009 | Unregistered Commenteremilcar » Reglas interes

[...] Link [...]

Hey, i have been looking all over for something kinda like your script, well at least part of it.
Mail's dictionary isn't very helpful i guess if you don't know what your looking at lol.
Anyway, i was hoping some one could help me out... I need to extract the name, if there is one, and the address, and then where you have it "say" something i would just leave it open so i could use the variables "the_sender" and my "the_address" for other things. I cant get it to extract the address no matter what I do.

so basically just this...
on perform_mail_action(info)
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
end perform_mail_action

February 11, 2010 | Unregistered CommenterE-z

Hey any comments are still appreciated, however I think I may have just done it, and I don't know how.

tell application "Mail"
set theSelection to selection
set theMessage to item 1 of theSelection
extract address from sender of theMessage
end tell

February 11, 2010 | Unregistered CommenterE-z

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>