Monday, August 1, 2011

Gmail snoozer

Chrome: One of our most wanted email innovations is the ability to "snooze" email messages like we do alarms. Snooze Your Email for Chrome does that for Gmail, alerting you to important messages later on—whether 5 minutes or 5 days.

Have you ever gotten an email that you know you need to act on, but just don't have the time right now? With Snooze Your Email, you can tell Chrome to send you a desktop notification after a specified period of time, reminding you to take another look at that email. It's a feature we've wanted to see in Gmail for awhile, but just hasn't shown up—so an unofficial Chrome extension is the next best thing.

Snooze Your Email Reminds You of Important Emails Later OnSnooze Your Email is a free download, works wherever Google Chrome does.....


The Solution

Here is the Apps Script code for a “Gmail Snooze” extension. First some configuration details. Setting these variables determines whether “unsnoozed” mail gets marked as unread, and whether it gets its own special label.
var MARK_UNREAD = false;
var ADD_UNSNOOZED_LABEL = false;

Setup

The “setup” function creates a new “Snooze” label in your Gmail, along with 7 sublabels for snoozing for different lengths of time, and potentially an “Unsnoozed” label. Running this function will also prompt you to authorize the script to use Gmail. This function makes use of the “getLabelName” helper function, which will be used by the code below.

Note: After you run the setup() function, the labels will be created in Gmail. However, you may have to refresh your Gmail window to see the labels.

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");
}
}

Moving the Snooze Queue

The “moveSnoozes” function moves messages one day forward in the queue, so that messages snoozed for 6 days are now snoozed for 5 days, etc. Messages in the 1-day label are moved back into the inbox, and potentially marked as unread. To make this work automatically, you’ll need to create a nightly event trigger to run “moveSnoozes”. See the more detailed instructions at the bottom of the post.
function 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);
}
}
}
}

Using Snooze Label in Gmail

To "snooze" a thread, use Gmail’s “Move To” button to move the thread into the "Snooze for X days" label and archive it. Every night, threads will move up through one day of the queue, and at the appointed number of days they will reappear in your inbox, unarchived. If you want the messages to reappear as unread, just change “MARK_UNREAD” at the top to be “true”.

Because this is an Apps Script, you can edit the code any way you like. If you’d like different snooze times or for unsnoozed messages to get starred, you can easily change the code. And if you have an even better idea for how to use Apps Script to improve Gmail, you can post it to our Gallery (Script Editor > Share > Publish Project) to share with the world.

If you don't know how to setup a script, it's pretty simple. Create a new Google Spreadsheet, and choose "Script Editor" from the "Tools" menu. Paste in all of the code from above and then click the “Save” button and give it a name. In the dropdown labeled "Select a function to run," choose "setup" and click the blue run arrow to the left of it. This will ask you to authorize the script, and will create the necessary labels in your Gmail. Then go to the "Triggers" menu and choose "current script's triggers." Click the link to set up a new trigger, choosing the "moveSnoozes" function, a "time-driven" event, "day timer," and then "midnight to 1am." Click save and you are done.

Friday, July 8, 2011