How to you access Apple Mail from Apple script

applescriptcronemailmail.app

I want to archive old emails ( say, older then 7 days ) from my inbox automatically with an applescript scheduled via cron.

To be honest, I am open to other Apple-friendly languages such as Swift or Coco , though the learning curve might be prohibitive for me.

Ideas, Examples, and links relevant to the topic are welcome.

Best Answer

To start with, you can try this Applescript:

set interval to 7
set target to (current date) - interval
set oldCount to 0
tell application "Mail"
    set box to mailbox named "INBOX" of account named "Your_mailbox_here"
    set maxMessages to count of messages in box
    repeat with index from 1 to maxMessages
        set currentMessage to message index in box
        set msgDate to date received of currentMessage
        if msgDate is greater than target then
            set oldCount to oldCount + 1
            # do something here
        end if
    end repeat
end tell

Enter the above script in Script Editor, play with it till it does what you want, then save it as a script file (.scpt). run it with osascript(1).

Look at Applescript Resources and the Applescript Language Guide for more useful info. Also look at the scripting dictionary of Mail.app using the "Open Dictionary..." command in Script Editor to see what sort of things you can script in mail.