Get messages that were sent in the last 7 days (Applescript)

applescriptmail.app

As part of my routine, I review all outgoing emails once every week to see if any of them need to be flagged for follow-up. I want to try a different approach and instead of going through my Sent Mail, automatically send myself an email every week that lists all emails sent in the preceding week.

I wrote an AppleScript that does this, but it contains an inefficiency that I'd like to remove.

repeat with myMessage in items 1 through theHorizon of (get messages of myMailbox)              
if (currentDate - (date sent of myMessage)) div days is less than theTimeframe then

As my Sent Mail has tens of thousands of messages, I make the script only consider the first 350 messages (theHorizon). This should be enough, but I don't really know. To confirm the message was indeed sent within the last 7 days, I run every message through an if statement.

What I would like, however, is for the repeat statement to read something like this

repeat with myMessage in (get messages of myMailbox where date sent is less than 7 days ago)

This obviously doesn't work, I've tried a bunch of different formats but nothing seems to be working properly.

Any thoughts?

Best Answer

Yes, that's absolutely possible. Here's an example using my IMAP mail account, the name of which I've redacted for personal privacy:

    tell application "Mail" to get the subject of ¬
        every message of mailbox "Sent" of ¬
        account "C*************.com" whose ¬
        date sent > ((current date) - days * 3)

This returns a list containing the subjects of each mail message that I sent within the last 3 days.

To apply this to your specific situation, you create a command that resembles this:

    tell application "Mail" to repeat with myMessage in (the ¬
        messages of mailbox MySentMailbox of ¬
        account MyAccount whose ¬
        date sent > ((current date) - days * 7))
            .
            .
            .
    end repeat