简体   繁体   中英

Applescript to archive email in Mail.app

I need to write an Applescript for Mail.app that will take all messages in my Inbox and Sent Messages that are older than a certain # of days and move them into respective folders "On My Mac", or local folders.

The reason for this being my IMAP account has a 120 day quota limit, and I'd rather automate "archiving" my email to a local folder rather than doing it manually.

What have you tried so far? Your question is very broad. The following should get you started:

property secondsIn120Days : 10368000

tell application "Mail"

    set theInbox to inbox

    set dateToday to current date

    set firstMessage to 1
    set lastMessage to (get count of messages in theInbox)

    repeat with thisMessage from lastMessage to firstMessage by -1
        set currentMessage to message thisMessage of theInbox
        set messageDate to date received of currentMessage

        set timeDifference to dateToday - messageDate

        if timeDifference ≥ secondsIn120Days then

            (* In answer to your comment, any folder you create to archive
            messages is going to be in the "On My Mac" directory. But say you
             create a Smart Mailbox called "Mail Archive"  then all you should 
            need are these lines... *)

            set archiveMailbox to (mailbox ("Mail Archive" as string))
            move currentMessage to archiveMailbox

        end if
    end repeat
end tell

UPDATE: Added response to comments in the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM