使用JXA清理Apple Mail文件夹

时间:2018-10-05 11:26:51

标签: macos jxa apple-mail

我想编写一个JXA脚本,该脚本会自动从我的文件夹中删除旧邮件。我找到了一个解决方案,但是它很慢,我想知道是否有一种方法可以加快速度。

这是我的剧本:

mailApp = Application("Mail");
mailApp.includeStandardAdditions = true;

var now = Date.now();

function calcDaysAge(date) {
    return ~~((now-date)/3600/24/1000) //~~((now - date) / (24*60*60*1000);
}

function deleteMessagesOf(mailbox, minDaysAge) {
    messages = mailApp.mailboxes.byName(mailbox).messages
    for (var i=messages.length-1; i>=0; i--) {
        message = messages[i]
        date = new Date(message.dateReceived())
        age = calcDaysAge(date)
        if (age > minDaysAge) {
            message.delete()
        }
    }
}

// delete mails older than 7 days from my Cronjobs mailbox.
deleteMessagesOf("Cronjobs", 7)

1 个答案:

答案 0 :(得分:0)

刚刚找到了类似问题的答案。这似乎效果更好,但是当要删除大量邮件时,仍然会超时:

mailApp = Application("Mail");
mailApp.includeStandardAdditions = true;

function deleteMessagesOf(mailbox, minDaysAge) {
    var cutoffDate = new Date(Date.now() - minDaysAge * 60 * 60 * 24 * 1000);
    messages = mailApp.mailboxes.byName(mailbox).messages.whose({ dateReceived: { '<': cutoffDate } })
    if (messages.length > 0) {
        mailApp.delete(messages)
    }
}

deleteMessagesOf("Cronjobs", 7)