Mail.app logo Getting back into blogging today I would like to tell you about a script that aims to improve the e-mail label management of Apple’s built in e-mail client Mail.app.

On the far away :-D july 2006 I had the possibility to follow an advanced course of Mac OS X in the classrooms of the Politecnico di Bari realized by the Campus Satellitare del Salento with the participations of the professor Franco Tommasi (link) and his staff, which counted my pal Paolo Portaluri.

Paolo {*}, in those last two years has become on of my top 2 Apple consultant, proving to be reliable, precise, always insightful and helping a lot!

At the time, howerever, our friendship was borning and I think one of the seeds was my question to him on how to have colored labels in Mail.app.

Since the beginning of my use of e-mail clients I had become used, thanks to Eudora first and Thunderbird now, to label with a name and a color some of e-mails present in my mailboxes.

Red for “important” e-mail, blue for the “link” containing one, green for e-mail containing addresses, purple for “humor” messages. You got the idea, I hope!

So Paolo told me that the application itself wasn’t giving me any option in that direction but that probably, using a simple AppleScript, he could give me a solution to the problem.

And so he did! And later on published the code in this post on the Apple’s themed TEVAC forum. One of the biggest italian resources on Cupertino’s stuff.

So what follows now is a traslation of the procedure and a modding of the script to my liking!

First you have to launch the Script Editor given with each copy of OS X.

The Script Editor application, located in /Applications/AppleScript, is a useful tool for working with Apple events. If your application is scriptable, you can use Script Editor to write and execute scripts that target your application, resulting in Apple events being sent to the application.

You will copy the following AppleScript and save it on your desktop with a name, personally I’ve used the labeler-mailapp.scpt name.

Here’s the code:
using terms from application "Mail"
on perform mail action with messages selectedMsgs
— let’s choose the colors
set chosenColor to item 1 of (choose from list {”Link”, “Gray”, “Address”, “Interesting”, “Humor”, “Important”, “Yellow”, “None”})
if chosenColor is equal to “Link” then
set color to blue
else if chosenColor is equal to “Gray” then
set color to gray
else if chosenColor is equal to “Address” then
set color to green
else if chosenColor is equal to “Interesting” then
set color to orange
else if chosenColor is equal to “Humor” then
set color to purple
else if chosenColor is equal to “Important” then
set color to red
else if chosenColor is equal to “Yellow” then
set color to yellow
else if chosenColor is equal to “None” then
set color to none
end if
tell application “Mail”
set selCount to (count of selectedMsgs)
if selCount is equal to 0 then
display dialog “NO SELECTED MESSAGE!” buttons {”OK”} default button 1 with icon stop
else
repeat with counter from 1 to selCount
set msg to item counter of selectedMsgs
— apply the color
set background color of msg to color
end repeat
end if
end tell
end perform mail action with messages
end using terms from
– If run as an ordinary script, instead of directly from the Scripts
– menu, it will call the default handler instead.
using terms from application “Mail”
on run
tell application “Mail” to set sel to selection
tell me to perform mail action with messages (sel)
end run
end using terms from

Saved? All OK?

Now let’s copy the scpt file into the ~/Library/Scripts/Applications/Mail/ directory (even if I now have it placed and working into ~/Library/Scripts/Applications/Mail Scripts).

Two more steps to go!

We have to lauch the Applescript Utility on your Mac and enable the “Script menu in to the menu bar“, this will show you a “paper” into the menu bar … please forgive the bad translation but I hope that the image here will show you what I mean.

Now we launch Mail.app, select the particular e-mail we want to label, click on the “paper” above and choose the voice “labeler-mailapp” and we’ll have a new window from where to choose the label/color intended.

That’s it!

Now a curiosity, if you reinstall your Mac and do not reinstall the script your e-mails will store the previously given color.

Cheers!


{*} yes, Paolo is poweruser82 that with his more than 22.000 posts on the Tevac’s forum is an envaluable resouces to us!


  1. kOoLiNuS

    Sorry about the loose identing of the code :-/

Leave a Comment