Created
October 22, 2013 14:48
-
-
Save mozz100/7102068 to your computer and use it in GitHub Desktop.
GMail snooze, with some mozz modifications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// see http://googleappsdeveloper.blogspot.co.uk/2011/07/gmail-snooze-with-apps-script.html | |
// Should be set to run every day at midnight, and moves emails one label 'nearer' to the inbox | |
var MARK_UNREAD = false; | |
var ADD_UNSNOOZED_LABEL = true; | |
function getLabelName(i) { | |
return "Snooze/Snooze " + i + " days"; | |
} | |
function setup() { | |
// Create the labels we’ll need for snoozing | |
GmailApp.createLabel("Snooze"); | |
for (var i = 1; i <= 7; ++i) { | |
GmailApp.createLabel(getLabelName(i)); | |
} | |
if (ADD_UNSNOOZED_LABEL) { | |
GmailApp.createLabel("Unsnoozed"); | |
} | |
} | |
function moveSnoozes() { | |
Logger.log("Running moveSnoozes..."); | |
var oldLabel, newLabel, page; | |
for (var i = 1; i <= 7; ++i) { | |
newLabel = oldLabel; | |
oldLabel = GmailApp.getUserLabelByName(getLabelName(i)); | |
page = null; | |
// Get threads in "pages" of 100 at a time | |
while(!page || page.length == 100) { | |
page = oldLabel.getThreads(0, 100); | |
if (page.length > 0) { | |
if (newLabel) { | |
// Move the threads into "today’s" label | |
newLabel.addToThreads(page); | |
} else { | |
// Unless it’s time to unsnooze it | |
GmailApp.moveThreadsToInbox(page); | |
if (MARK_UNREAD) { | |
GmailApp.markThreadsUnread(page); | |
} | |
if (ADD_UNSNOOZED_LABEL) { | |
GmailApp.getUserLabelByName("Unsnoozed") | |
.addToThreads(page); | |
} | |
} | |
// Move the threads out of "yesterday’s" label | |
oldLabel.removeFromThreads(page); | |
} | |
} | |
} | |
Logger.log("moveSnoozes finished."); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment