Google Apps Script

Just having a play with some Google apps scripting.


// Nothing is deleted just archived.
//
// Look in All Mail if you don't see it in the InBox.
//
// Script runs each night between 11pm and midnight.
//
// Archives mail in the inbox that is more than two days old both read and unread.
//
// If you want something not to be archived give it a yellow star.
//
function archiveInbox() {
var threads = GmailApp.search('label:inbox older_than:2d -has:yellow-star');
for (var i = 0; i < threads.length; i++) {
threads[i].moveToArchive();
Utilities.sleep(5000);
}
}

function archiveTrigger() {
ScriptApp.newTrigger("archiveInbox")
.timeBased()
.atHour(23)
.everyDays(1) // Frequency is required if you are using atHour() or nearMinute()
.create();
}

All this good info came from here…

 

Below is the improved version….


// Created this script as the shared garden inbox was filling up with rubbish.
//
// Nothing is deleted just archived.
//
// Look in All Mail if you don't see it in the InBox.
//
// Script runs each night between 11pm and midnight.
//
// Archives mail in the inbox that is more than two days old both read and unread.
//
// If you want something not to be archived give it a yellow star.
//
function archiveInbox() {
var inThreads = GmailApp.search("'in:inbox older_than:2d'");
for (var i = 0; i < inThreads.length; i++) {
inThreads[i].moveToArchive()
Utilities.sleep(3000);
}
var sentThreads = GmailApp.search("'in:sent newer_than:2d'");
for (var j = 0; j < sentThreads.length; j++) {
sentThreads[j].moveToInbox();
}
var starThreads = GmailApp.search('is:starred');
for (var k = 0; k < starThreads.length; k++) {
starThreads[k].moveToInbox();
}
}

function archiveTrigger() {
ScriptApp.newTrigger("archiveInbox")
.timeBased()
.atHour(23)
.everyDays(1) // Frequency is required if you are using atHour() or nearMinute()
.create();
}

Leave a Reply

Your email address will not be published. Required fields are marked *