-- Move messages to a folder based on the date sent, creating the folder if needed! -- Warning! This is my first ever applescript! Nicholas Cole n@npcole.com -- Copyright Nicholas Cole 2005 -- www.npcole.com/moveemail.html (* Documentation This script is most useful when combined with the plugin "Act-On", which allows you to assign mail rules (and therefore applescripts) to key presses. For information on setting up a local archive of your email with IMAP see: www.npcole.com/usingimaptiger.html The basic function of this script is to move messages highlighted in mail.app into the appropriate archive folder. For IMAP accounts this is a mailbox called YYYY-MM-Mail, where YYYY is the year the message was sent and MM is the two digit code for the month. For other accounts this is a local folder, named in the same way. If you have an IMAP account called in mail "Archives" or "Local Account" or "Local Archives" some additional features become available. The script will in that case not use local folders, but will instead use the archives to store non-IMAP mail. Mail in the archives will be stored under a mailbox folder called archives-XXXX where XXXX is the account name. Under this folder, mail is sorted by month as before. If the relevant month folder already exists here, mail that would normally be archived on the IMAP server is put onto the archives account instead. In this way, if you've already moved a folder from your main IMAP server to your archives, the mail is stored in the right place. *) set possible_archive_names to {"Local Account", "Local Archives", "Archives"} using terms from application "Mail" tell application "Mail" -- Find imap archives if any set MY_ARCHIVES to "" repeat with possName in possible_archive_names if exists account possName then if (account type of account possName is imap) then set MY_ARCHIVES to possName end if end repeat set theMessages to selection repeat with eachMessage in theMessages set theDate to (date sent of eachMessage) set theYear to (year of theDate) set theMonth to (month of theDate) set theMonthNumber to month of theDate as number if theMonthNumber < 10 then set theMonthNumber to ("0" & theMonthNumber) set Acct to (name of (account of (mailbox of eachMessage))) set ProperMailBox to ((theYear & "-" & theMonthNumber & "-Mail") as string) set ProperArchiveBox to "" if (MY_ARCHIVES is not "") then set archiveBox to ("archives-" & Acct) as string set ProperArchiveBox to ((archiveBox & "/" & ProperMailBox) as string) --ensure archive folders exist tell account MY_ARCHIVES if (not (exists mailbox archiveBox)) then -- nasty hack to get around the fact that mail.app doesn't like creating folder hierarchy make new mailbox at beginning with properties {name:(archiveBox & "/" & ".youcandeletethisbox")} delete mailbox (archiveBox & "/" & ".youcandeletethisbox") end if end tell end if if (account type of account Acct is imap) then if (not (exists mailbox ProperMailBox)) then if ((MY_ARCHIVES is not "") and (exists mailbox ProperArchiveBox of account MY_ARCHIVES)) then my moveEmail(eachMessage, ProperArchiveBox, account MY_ARCHIVES) else my moveEmail(eachMessage, ProperMailBox, account Acct) end if end if else if MY_ARCHIVES is not "" then -- move non IMAP mail into imap archives my moveEmail(eachMessage, ProperArchiveBox, account MY_ARCHIVES) else -- store mail locally instead my moveEmail(eachMessage, ProperMailBox, application "Mail") end if end repeat end tell end using terms from on moveEmail(theMessage, theFolderName, objectToTell) tell application "Mail" tell objectToTell if not (exists mailbox (theFolderName as string)) then make new mailbox at beginning with properties {name:(theFolderName as string)} end if move theMessage to mailbox theFolderName end tell end tell end moveEmail