Sunday, 1 January 2006

AppleScript/Mail Archive

Based on Delfim's stuff and my previous tinkering here's an AppleScript that stores e-mails under a Personal/Year/Quarter tree.

(Despite having several accounts configured, I archive everything under my Home account.)

It is designed to be activated from a Mail.app rule (or via Mail Act-On):

using terms from application "Mail"
  on perform mail action with messages theMessages for rule theRule
    tell application "Mail"
      repeat with thisMessage in theMessages
        set msgDate to date received of thisMessage
        set msgMonth to month of msgDate as integer
        set msgYear to year of msgDate as integer
        set msgQuarter to ((round ((msgMonth - 1) / 3) rounding down) + 1)
        set msgAccount to name of account of mailbox of thisMessage
        set msgMailbox to name of mailbox of thisMessage
        set mboxName to "Personal/" & msgYear & "/Q" & msgQuarter
        tell account "Home"
          try
            set mbox to mailbox named mboxName
            get name of mbox
          on error
            make new mailbox with properties {name:mboxName}
            set mbox to mailbox named mboxName
          end try
          move thisMessage to mbox
        end tell
      end repeat
    end tell
  end perform mail action with messages
end using terms from