How to Tag Mail Messages Using AppleScript and Tagger

avatar image

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.

Tagging Mail for Mac Users: Get Mail Tags for Free With Tagger
Photo by campbelj45ca - http://flic.kr/p/68H3rL

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.

About the Author: With an educational background in philosophy and mathematics, as well as in counselling, Dr Mulhauser enjoys publishing CounsellingResource.com, providing online counselling and therapy services, and spending time with his family.

This article was last reviewed by Dr Greg Mulhauser, Managing Editor on Tuesday, 17th November 2009.

The URL of this page is:
http://counsellingresource.com/features/2009/11/17/tagging-mail-mac-users/

There are no comments yet on this article — would you like to be the first to post a response?

Join the Discussion!

We support Gravatars rated PG or G; if you don't have a Gravatar, we'll display a mathematically created identicon next to your comment.

A valid email address is required to enable you to personally verify and authorize your comment for posting. It will not be displayed in your post or used in any other way. SPAM comments will be deleted, as will those attempting to circumvent the Gravatar rating system or attempting to circumvent our Terms of Use by employing Gravatar images to advertise on this site.

 characters available

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe without commenting