1

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.

Community
  • 1
  • 1
madsteer
  • 71
  • 6
  • Figure out how to write it in AppleScript first. If it works in AS, it's JXA being busted and crap. If it doesn't work in AppleScript, ask here and on the [AppleScript-users](https://lists.apple.com/mailman/listinfo/applescript-users) mailing list as it's probably a Mail/Exchange issue. – foo Aug 01 '16 at 19:10
  • 1. Nothing I said about the extra complexities of my company's exchange server is any different for AppleScript. 2. AppleScript and JXA JavaScript are equally poorly documented in the script editor tool. 3. At least normal JavaScript is much better documented via the internet, so I thought this might be a good way to keep my JavaScript skills up to date. Perhaps not. – madsteer Aug 02 '16 at 12:53
  • [AppleScript Language Guide](https://developer.apple.com/library/mac/documentation/AppleScript/Conceptual/AppleScriptLangGuide). [JXA release notes](https://developer.apple.com/library/mac/releasenotes/InterapplicationCommunication/RN-JavaScriptForAutomation). There's also several AS books (I coauthored the Apress one), though nothing for JXA (I scrapped plans to do one for obvious reasons). See also [Dr Cook's paper on AS's early history](http://www.cs.utexas.edu/~wcook/Drafts/2006/ashopl.pdf) and quick-n-dirty [Apple event overview](http://hhas.bitbucket.org/understanding-apple-events.html). – foo Aug 03 '16 at 10:19
  • Again, if you want to figure out why your 'move to trash' command isn't working you need to troubleshoot it at your end; and since JXA's Apple event support is known to be flawed the logical first step is to try the same command in AppleScript to see if it works there. Also, scriptable apps like Mail are notoriously under-documented, so search the web for existing Mail scripting discussions and examples as experienced AppleScripters will no doubt have done stuff like this before and figured out the right and wrong ways to do it. Good luck. – foo Aug 03 '16 at 10:35

0 Answers0