I asked this question a few months ago. @foo and @bacongravy posted some comments below my solution that I found interesting. Rather than continue those comments (wouldn't fit in the text limit of a comment), I thought I'd ask a new, but similar, question. I'm wanting to delete messages from the inbox of a certain age. @bacongravy commented:
var cutoffDate = new Date(Date.now() - 44 * 60 * 60 * 24 * 1000);
Application("Mail").move(Application("Mail").inbox.messages.whose({ dateSent: { '<': cutoffDate } }), {to: Application("Mail").mailbox["Archive"])
The problem is two pronged. One prong is my Apple Mail is connected to a Microsoft Exchange server managed by an enterprise Microsoft Active Directory. In both AppleScript and JavaScript, this seems to complicate scripting things for Mail. I constantly have to reference the correct "account" even though I have only one. So, I did manage to get this to work:
var cutoffDate = new Date(Date.now() - 44 * 60 * 60 * 24 * 1000);
Application("Mail").move(Application("Mail").inbox.messages.whose({ dateSent: { '<': cutoffDate } }), {to: Application("Mail").accounts.byName("Company Exchange").mailboxes.byName("Deleted Items")});
However, I'm hesitant to stop here, because of prong 2: The account name "Company Exchange" seems to change periodically over time. I've seen it just called "Exchange" at times, and then at other times (like now) it's "Company Exchange". The only constant I've found so far is the word "Exchange" is in the name somewhere.
To address this, I've been in the habit of doing the following:
var Mail = new Application("Mail")
var account = Mail.accounts.whose({
name: { _contains: 'exchange' }
}, {
ignoring: 'case'
});
var inbox = account.mailboxes.whose({
name: { _contains: 'inbox' }
}, {
ignoring: 'case'
});
var trash = account.mailboxes.whose({
name: { _contains: 'deleted items' }
},{
ignoring: 'case'
});
However,
Mail.move(inbox.messages.whose({ dateSent: { '<': cutoffDate }}), {to: trash});
didn't seem to work. Just wondering if it can't work, or if I have a syntax error that is preventing it from working.