Create a Thunderbird-like mail archiving feature in Mail

Feb 23, '10 07:30:01AM

Contributed by: hibbelig

In my recent Thunderbird 3 usage, I've come to rely on search almost exclusively. I leave messages in my inbox until they are dealt with, and when they are dealt with, I just hit the 'A' key to archive them. Now I can do that with Mail.app, too.

When you hit 'A' in Thunderbird, it archives each message in a folder based on the date of the message, e.g. INBOX/Archives/2010 for messages from this year. The following AppleScript also does this in Mail. (The script will also mark archived messages as read -- Thunderbird's 'A' key does not do that.)

Copy and paste the following into AppleScript Editor:

on run
  tell application "Mail"
    set selectedMessages to selection
    repeat with selectedMessage in selectedMessages
      set msgDate to date sent of selectedMessage
      set msgYear to (year of msgDate) as string
      set mbox to mailbox of selectedMessage
      set acct to account of mbox
      set archivePath to "Archives/" & msgYear
      try
        set archiveMbox to mailbox archivePath
      on error theError
        make new mailbox with properties {name:archivePath}
        set archiveMbox to mailbox archivePath
      end try
      set read status of selectedMessage to true
      move selectedMessage to archiveMbox
    end repeat
    set nextBox to mailbox of last item of selectedMessages
    set nextList to messages of nextBox whose deleted status is false
    set nextMsg to my findNext(selectedMessages, nextList)
    set message viewer 1's selected messages to {nextMsg}
  end tell
end run

on findNext(selectedMessages, allMessages)
  set len to count of allMessages
  set idx to 1
  repeat while idx ≤ len
    set candidate to item idx of allMessages
    if selectedMessages contains {candidate} then exit repeat
    set idx to idx + 1
  end repeat
  if idx > 1 then
    return item (idx - 1) of allMessages
  else
    return last item of allMessages
  end if
end findNext
Save it into your user's Library » Scripts » Applications » Mail folder, and name it Archive selected messages.scpt. I use FastScripts to assign the shortcut Command-Shift-9 to this script (by analogy of the nice MsgFiler).

Problem: this does not always select the right message after archiving. If you have any ideas about that, please share.

[robg adds: This should work in both 10.5 and 10.6; I tested it in 10.5 (where it worked perfectly), and the author refers to AppleScript Editor, which is the new name for Script Editor in 10.6.]

Comments (6)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20100221135218172