Search code examples
applescriptapple-mail

Applescript Mail archive Actions


So, I finally got tired of manually sorting and archiving email on my Mac to get around some archaic mailbox size limits. I'm not a fan of the way Apple handles mail archiving, so I dug into a few examples that others had written to sort mail and slapped a short script together to filter through my email and archive everything older than 90 days. For the most part, easy stuff.

It is a tad annoying that Mail doesn't see "On My Mac" as a local account, so you need to adjust the code to blindly reference mailbox names.. but.. it works.

I think I'm just having an evaluation problem. I can sort the "Inbox" and archive mail from there quite easily.

    on archive_email(target_account, target_mailbox, destination_account, destination_mailbox, oldemaildate)

    tell application "Mail"

        #Identify messages that need to be moved. If unread messages shouldn't be moved, change the end of the variable definition to read "is less than oldsentdate and read status is true)
        set _msgs_to_capture to (every message of mailbox target_mailbox of account target_account whose date received is less than oldemaildate)

        repeat with eachMessage in _msgs_to_capture

            set archiveMailbox to (mailbox (destination_mailbox as string))
            move eachMessage to archiveMailbox

        end repeat

end archive_email

This doesn't work for the "Sent" folder, however. My guess is that the evaluation "whose date received is less than" is improper for the "sent" folder. Sadly, "whose date sent is..." doesn't seem to work either. I'm struggling to find the right variable and evaluation here.


Solution

  • The line bellow is perfectly working, assuming target_account and oldmaildate have correct values. To be sure, I replaced target_mailbox directly by "Sent Messages" :

    set _msgs_to_capture to every message of mailbox "Sent Messages" of target_account whose date sent is less than oldemaildate
    

    Please check values/types of your variables.

    Also, I suggest you set the line below before the repeat loop and not inside, to increase speed :

    set archiveMailbox to (mailbox (destination_mailbox as string))