Skip to content

Instantly share code, notes, and snippets.

@dreeves
Created September 8, 2012 00:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dreeves/3671023 to your computer and use it in GitHub Desktop.
Save dreeves/3671023 to your computer and use it in GitHub Desktop.
Email Snooze
/* EMAIL SNOOZE
Inspired by: http://lifehacker.com/5825634/how-to-add-a-snooze-button-to-gmail
and http://messymatters.com/email
Docs: https://developers.google.com/apps-script/service_docslist
Initial Setup Instructions:
Create a new google docs spreadsheet (sic) and then select
Tools > Script Editor
and paste in this code.
Then create a trigger (in the Resources menu) to run the shiftaroo function
nightly, just after midnight.
Email Snooze:
All the magic is in the gmail labels, using the standard gmail interface.
A thread with a single integer as a label, say "7", will reappear in the inbox
in 7 days. To snooze for 7 days, type: v7<enter>. That moves the thread from the
inbox to the label "7".
Re-ping:
To get the most out of this awesomeness you should create a filter that puts
your own outgoing messages in your inbox. That sounds annoying but it's just two
more keystrokes to get a message back out ("xy" -- select and archive). The
advantage is that if you want to be reminded to follow up, in case the recipient
drops the ball, you can then snooze it like any other message.
The idea is that all active conversations should initially be in your inbox
until you explicitly allow them to go out of sight, out of mind.
Here's how to create the filter:
1. Create a filter (available from the "show search options" dropdown on the
search box).
2. Set the From to your own email address, or your username -- something that
will match all the email addresses you send from.
3. On the next screen, check nothing but "Never send it to Spam".
Auto-expire idea (not yet implemented):
A thread with a label like "xNN" (where NN is a number of days) will
automatically have NN decrement nightly until it hits zero, when the label will
become just "x" (indicating auto-expired) and it will disappear from the inbox.
*/
// Returns the max integer label, ie, max number of days of snooze.
function smax() {
var all = GmailApp.getUserLabels();
var smax = 0, s, x;
for(var i = 0; i < all.length; i++) {
s = all[i].getName();
if(s.match(/^\d+$/)) {
x = parseInt(s);
if(x > smax) smax = x;
}
}
return smax;
}
// Shift snooze labels; ..., 3->2, 2->1, 1->inbox (triggered nightly).
function shiftaroo() {
var li, limo, page; // label for i, label for i minus one, an array of threads
var x = smax();
for(var i = 1; i <= x; ++i) {
limo = li; // limo is the previous label, i-1
li = GmailApp.getUserLabelByName(i);
if(!li) continue;
page = null;
while(!page || page.length==100) { // get threads in pages of 100 at a time
page = li.getThreads(0,100);
if(page.length > 0) {
if(i==1) { // shift things labeled "1" to the inbox
GmailApp.moveThreadsToInbox(page);
} else { // shift things labeled i to label i-1
if(!limo) limo = GmailApp.createLabel(i-1);
limo.addToThreads(page);
}
li.removeFromThreads(page);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment