How to Tag Mail Messages Using AppleScript and Tagger
If you’re a Mac user looking for a way to help organise your Mail messages using the OpenMeta-style tags we described here recently, you can easily connect Mail under Leopard or Snow Leopard with Ali Rantakari’s free Tagger application.

UPDATE: Thanks to an extremely responsive developer, the AppleScript solution described on this page has now been integrated directly into the Tagger application itself. See “Mail Tagging Now Integrated Into Tagger for Mac Users”.
Basic — and Entirely Free — Mail Tags
Not long ago, we reviewed the OpenMeta tagging tool called Leap. It provides a powerful and flexible way of using OpenMeta tags to keep track of your digital stuff — just about any kind of stuff. And let’s face it: we all have enough stress in our lives without wasting unnecessary time just trying to keep track of stuff.
Anyway, in that review, I happened to mention that my own favourite tool for applying tags to files is a nifty little application which also happens to be entirely free: Tagger, by Ali Rantakari. Although Tagger is designed to enable tagging with document-based applications, several people have asked whether it’s possible to tag individual email messages with something like Tagger.
It turns out that it’s entirely possible, and all you need is a bit of AppleScript glue to connect up Apple’s Mail with Tagger. (What’s that, you say? There’s a whole piece of software designed specifically for tagging email, and it happens to be called MailTags? Why yes, there is — comments on MailTags to follow below.)
This script makes use of three helpful facts:
- Tagger provides a command line interface, which can be accessed via AppleScript, and
- Mail can be used to find the unique ID of any given message, and
- Spotlight can be used to locate the individual file which corresponds to a given message ID pretty quickly.
So with those in mind, here’s the snippet of AppleScript that acts as ‘glue’:
tell application "Mail"
set msgURL to ""
set founderror to ""
repeat with msg in (selection as list)
set theId to id of msg
set myURL to (do shell script "mdfind -onlyin ~/Library/Mail \"kMDItemFSName = '" & theId & ".emlx'\"")
if myURL is "" then
set testURL to (do shell script "mdfind -onlyin ~/Library/Mail \"kMDItemFSName = '" & theId & ".partial.emlx'\"")
if testURL is not "" then
set founderror to "One or more of the selected messages has only been partially downloaded. All messages to be tagged must be downloaded fully for tagging to work reliably."
else
set founderror to "Spotlight was unable to find a local copy of the selected message."
end if
exit repeat
else
set myURL to quoted form of (POSIX path of myURL)
set msgURL to msgURL & " " & myURL
end if
end repeat
end tell
if founderror is not "" then
display dialog founderror with title "Whoops!" buttons {"OK"} default button 1
else
tell application "Finder" to set appPath to application file id "org.hasseg.Tagger" as string -- grab path using app file ID for Tagger
set command to quoted form of (POSIX path of appPath) & "/Contents/MacOS/Tagger -f " & msgURL
do shell script command
end if
What’s happening here?
The code runs through the set of messages which are currently selected in Mail, grabs the unique ID for each, and then attempts to find the individual file where that message is stored using Spotlight. It throws up an error if the message file cannot be found, or if the message hasn’t yet been fully downloaded. (In the latter case, tags get wiped out when the file is finally downloaded fully, so it’s no good trying to tag messages before then.)
Once it has the locations for each message, it hooks them all together and hands them off to Tagger using the latter’s command line interface. There’s a bit of jiggery-pokery at the end of the script which uses the Finder to figure out where exactly Tagger lives, using its own application ID, just in case you’ve renamed it or placed it somewhere unusual.
And that’s all there is to it!
How can you use it? You can download Greg’s Mail Tagging directly or copy the code above and drop it into your script editor. If you’ve enabled your AppleScript menu, you can save the resulting script to Mail’s script folder, usually ~/Library/Scripts/Applications/Mail, or — even better — you can drop it straight into a keyboard shortcut utility like Keyboard Maestro, where you can invoke it with a single hot key. In fact, you’ll see the tagging window much more quickly by dropping it straight into something like Keyboard Maestro or the free hot key application Spark; for some unknown reason, there’s a big delay when executing the script directly from Mail’s scripts menu.
Have fun!
What About THE MailTags?
I mentioned above that there’s a whole piece of software designed for tagging Mail on Macs, and it’s called MailTags. It’s by Indev Software, makers of the thoroughly outstanding Mail Act-On, which I use dozens of times per day. MailTags provides a vast amount of functionality which this AppleScript does not, including integration with Mail Act-On and the ability to send tags along with emails. It does that through a whole extra tagging system, which gets mirrored out to the OpenMeta system for the sake of OpenMeta compatibility.
If you need the sophisticated functionality of MailTags, there’s really no other way to go, and I’d encourage anyone to check it out. For my own personal uses, however, I don’t really like the MailTags interface (although I love the Mail Act-On interface!), and it feels like over-kill for what I’d like to achieve with tagging. Maybe after I’ve been tagging for awhile longer, I’ll change my tune and discover that basic file-level OpenMeta tagging is just too limited for my email needs. But for right now, with my limited aims for tagging, I think this simple little AppleScript will do just fine.
Your mileage may vary, of course, and I’m sure both approaches to tagging will happily co-exist for a long while.
Fine Print for Greg’s Mail Tagging Script
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 3 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

I’ve been using Tagger for the past week along with a few smart folders that I set up to parse emails with specific tags. My workflow is as follows:
- Email arrives in my inbox
- I read it
- I tag it (using tagger and an application launcher triggered by a keyboard shortcut)
- I move the tagged email to a folder called “Archive”
- Several Smart Folders are set up to parse my email by tag.
Only I’ve found a problem:
I’ve noticed lately that if I tag an email while in my inbox, the tag is removed when I move the email out of my inbox and into my “Archive” folder.
Does anyone know of a way around this? Why are the tags disappearing?
Hi Elijah,
I’d give Ali Rantakari a shout over at:
http://hasseg.org/tagger/
He’s really on top of this stuff and would probably be glad to know just in case what you’re experiencing should happen to turn out to be a bug.
All the best,
Greg
Already spoken to Ali. Since he didn’t write the script or use Mail.app, he didn’t really have an answer, but this is what I’ve been able to find out.
It’s not necessarily a “bug”, but more just a result of how Mail.app handles the files representing each of your emails. A number of .emix file representing each of your mail messages (drafts, sent, received, etc.) are all located in corresponding folders here:
~/Library/Mail/
I did some trial and error and it appears that whenever an email is moved to a new folder, or a draft email is saved/autosaved, Mail.app doesn’t just move the .emix file…it creates an entirely new file with a different name and deletes the old one (e.g. what was “1452.emix” is deleted and a new file named “1453.emix” takes its place). Since technically it’s a new file, any tags associated with that email are lost.
I’m not sure if there’s a way to adjust the applescript embedded into Tagger to somehow overcome this obstacle. If Mail moved/copied and renamed the .emix file, it wouldn’t be a problem.
If anyone knows of a solution, or a way to adjust the applescript to circumvent or make up for this behavior, that would be amazing, but I’m guessing it’s not possible using Openmeta tagging.
Hi Elijah,
Hmmm, this isn’t what I experience at all — provided that a message has already been fully downloaded (as noted in the post), I haven’t found that moving messages between mailboxes has any impact at all on their tags. My tests were performed by 1) manually dragging messages from one mailbox to another, 2) using Mail Act-On rules to move them, and 3) using Mail’s own rules to move them. In all cases, the tags very definitely did not change, irrespective of whether the underlying message file wound up with a new name in its new location. If you’re using a third-party tool to move messages, perhaps it’s doing something differently than Mail itself does?
All the best,
Greg
Hmmm…weird. To move emails I’ve just been either manually dragging and dropping from my inbox into a different folder, or using “Message > Move to…” from the pulldown menu within Mail.
To tag the emails, I’ve been using Google Search Box (application launcher) to invoke tagger. I then type in my tag, and hit enter twice.
What version of Mail are you using? Here are my specs:
Macbook Pro
2.53 Ghz Intel COre 2 Duo
OS X 10.6.3
Mail.app
Version 4.2 (1078)
Tagger
Version 1.2.3
Hi Elijah,
Hmm, that is weird. Yep, same software versions, except I’m using Keyboard Maestro to invoke Tagger. My only suggestions would be to 1) verify whether tagging is definitely working fine before you move messages, 2) check that messages are definitely downloaded in full to your local machine and are staying on your local machine (tagging will work if they are not, but they will be lost as soon as they are fully downloaded) and not still sitting on the server or being moved back to it, 3) check whether this happens only with your ‘Archive’ folder or whether it applies to any folder anywhere, and 4) be extra extra extra sure, by looking directly at the underlying files in the Finder (e.g., using QuickLook) that the before and after files really are identical except of course for the missing tags.
I have the feeling it’s somehow related to files getting moved onto or off of the server, or otherwise being treated in some unusual way by the moving process.
The other wildcard is that word ‘Archive’… Mail has an archiving feature, which I don’t personally use and so can’t comment on, but it might be all down to your ‘moved’ messages actually being ‘archived’, and ‘moving’ not being the same thing as ‘archiving’…
All the best,
Greg
OK. So I took a nap between my last comment and now and figured out what was happening in the process. Sometimes you just need to let the subconscious mind go to work on the problem. :)
You were right, the problem had to do with files being moved on and off the server. The folders I had created were located on the company exchange server (NOT on my local machine), so every time I would move an email message from my inbox to the folder and back again, it was in essence downloading and uploading the file again.
I’ve since created a local folder which I plan to move all my email to once they’re tagged. As email in my inbox is read and no longer useful for anything but later reference, I’ll tag it and then move it to this local folder to keep my inbox clear of clutter. I already have smart folders set up to parse emails by tag.
I’m not too familiar with the relationship between exchange servers and email clients…when emails are stored locally on the machine, are they no longer kept on the server? Is there any reason why keeping my “archive” folder local would pose a problem down the road? The only potential things I can think of are:
1) storage limitation
2) lack of backup should something happen to my laptop
3) accessibility if ever the laptop were stolen
Thoughts?
Hi Elijah,
I’m glad to hear you’ve cracked the problem — it was server-related all along!
I haven’t used Exchange for the better part of a decade, so I can’t offer any informed comment about the options available to you when it comes to local vs. server storage. It might be possible to store in both places, but with proviso that only local copies will retain Mac-based tags (which are, after all, maintained in the Unix filesystem, and so wouldn’t be expected to survive a transfer through the guts of Microsoft Exchange).
Assuming that the Exchange server is run by your employer, they might well have some policies in place regarding data retention and archiving, so it might be worth checking on.
All the best,
Greg
Thanks Greg! Appreciate all your help…and sorry for the long comment chains. :)