Created
July 29, 2017 08:51
-
-
Save arkarkark/7d13e4f6fe7b5185c56585c81a842b00 to your computer and use it in GitHub Desktop.
Gmail Snooze
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
// Copyright 2017 Alex K (wtwf.com) | |
// based on GMAIL Snooze https://gmail.googleblog.com/2011/07/gmail-snooze-with-apps-script.html | |
// This source is at https://wtwf.com/snoozesrc | |
// Add it from the chrome web at https://wtwf.com/snooze | |
DAY_LABELS = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] | |
SKIP_LABELS = ['1 week on', '2 weeks on', '3 weeks on', '4 weeks on'] | |
TIME_OF_DAY_LABELS = ['Morning', 'At Work', 'Tonight', 'Midnight'] | |
TRIGGERS = { | |
'morning': 5, | |
'atWork': 10, | |
'tonight': 17, | |
'midnight': 23, | |
} | |
function morning() {handle(TIME_OF_DAY_LABELS[0])} | |
function atWork() {handle(TIME_OF_DAY_LABELS[1])} | |
function tonight() {handle(TIME_OF_DAY_LABELS[2])} | |
function midnight() {handle(TIME_OF_DAY_LABELS[3])} | |
createTimeDrivenTriggers = function() { | |
triggers = JSON.parse(JSON.stringify(TRIGGERS)) | |
ScriptApp.getProjectTriggers().forEach(function(t) { | |
delete triggers[t.getHandlerFunction()] | |
}) | |
Object.keys(triggers).forEach(function(functionName) { | |
ScriptApp.newTrigger(functionName).timeBased().atHour(triggers[functionName]).everyDays(1).create() | |
}) | |
} | |
setup = function() { | |
// Create the labels we’ll need for snoozing | |
GmailApp.createLabel(getLabelName()); | |
makeNestedLabel('Day', DAY_LABELS) | |
makeNestedLabel('Skip', SKIP_LABELS) | |
makeNestedLabel('Time', TIME_OF_DAY_LABELS) | |
createTimeDrivenTriggers() | |
} | |
isToday = function(labelNames, todayLabel) { | |
return thisOrNone(labelNames, todayLabel, getLabelNames('Day', DAY_LABELS)) | |
} | |
isRightTime = function(labelNames, timeLabel) { | |
return thisOrNone(labelNames, timeLabel, getLabelNames('Time', TIME_OF_DAY_LABELS)) | |
} | |
isSkippy= function(thread, labelNames, doIt) { | |
skipLabels = getLabelNames('Skip', SKIP_LABELS) | |
done = false | |
skipLabels.forEach(function(skipLabel, skipIndex) { | |
if (done) return | |
labelNames.forEach(function(labelName) { | |
if (done) return | |
if (skipLabel == labelName) { | |
if (doIt) { | |
thread.removeLabel(GmailApp.getUserLabelByName(skipLabel)) | |
if (skipIndex > 0) { | |
thread.addLabel(GmailApp.getUserLabelByName(skipLabels[skipIndex - 1])) | |
} | |
} | |
done = true | |
} | |
}) | |
}) | |
return done | |
} | |
getLabelName = function(sub, name) { | |
arr = ['Snooze'] | |
if (sub) arr.push(sub) | |
if (name) arr.push(name) | |
return arr.join('/') | |
} | |
makeNestedLabel = function(sub, arr) { | |
GmailApp.createLabel(getLabelName(sub)) | |
arr.forEach(function(x) {GmailApp.createLabel(getLabelName(sub, x))}) | |
} | |
handle = function(timeFrame) { | |
setup() | |
timeLabel = GmailApp.getUserLabelByName(getLabelName('Time', timeFrame)) | |
todayLabel = GmailApp.getUserLabelByName(getLabelName('Day', DAY_LABELS[new Date().getDay()])) | |
handled = {} | |
handleLabel(timeLabel, timeLabel, todayLabel, handled) | |
handleLabel(todayLabel, timeLabel, todayLabel, handled) | |
} | |
thisOrNone = function(labelNames, wantedLabelName, allLabelNames) { | |
count = 0 | |
wanted = false | |
labelNames.forEach(function(labelName) { | |
if (wantedLabelName == labelName) wanted = true | |
allLabelNames.forEach(function(allLabelName) { | |
if (allLabelName == labelName) count++ | |
}) | |
}) | |
if (wanted) return 1 | |
if (count == 0) return 2 // nothing matched | |
return false | |
} | |
getNames = function(arr) {return arr.map(function(x) { return x.getName()})} | |
getLabelNames = function(sec, arr) {return arr.map(function(x) { return getLabelName(sec, x)})} | |
handleLabel = function(label, timeLabel, todayLabel, handled) { | |
var page = null; | |
while(!page || page.length == 100) { | |
page = label.getThreads(0, 100); | |
page.forEach(function(thread) { | |
labelNames = getNames(thread.getLabels()) | |
if (!handled[thread.getId()] && isToday(labelNames, todayLabel.getName())) { | |
rightTime = isRightTime(labelNames, timeLabel.getName()) | |
if (rightTime) { | |
handled[thread.getId()] = true; | |
doIt = timeLabel.getName() == getLabelName('Time', TIME_OF_DAY_LABELS[0]) | |
if (isSkippy(thread, labelNames, doIt)) { | |
return | |
} else { | |
thread.removeLabel(timeLabel) | |
thread.removeLabel(todayLabel) | |
thread.moveToInbox() | |
thread.markUnread() | |
} | |
} | |
} | |
}) | |
} | |
} | |
// Make this a WebApp so I can add it to the store and people can find it and run it | |
function doGet(e) { | |
morning() | |
return HtmlService.createTemplateFromFile('index.html').evaluate() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment