Search This Blog

Google Analytics

Sunday, July 31, 2011

Gmail Snooze with Apps Script

Google Apps Developer Blog published a snippet of code on that will help users "snooze" specific emails in Gmail, removing them from the Inbox and bringing them back for reading later.

What is Gmail Snooze?

One feature that some of us really wanted was for Gmail to let you “snooze” an email. Snoozing means archiving an email for now, but having it automatically reappear in the inbox at some specified time in the future. With Apps Script you can extend Gmail to add this functionality and a lot more yourself.


How to set it up?

1. Go to Google Docs and create a new spreadsheet, then choose "Script editor" from the "Tools" menu.

2. Save the spreadsheet.

3. Paste the following code snippet in the "Script editor".

var MARK_UNREAD = false;
var ADD_UNSNOOZED_LABEL = false;

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() {
  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);
      }  
    }
  }
}

4. Save script.

5. Execute setup() function from Run → setup menu. This will create the Snooze labels.

6. Setup a new trigger from Triggers → Current project's triggers menu, choosing the moveSnoozes() function, a "time-driven" event, "day timer," and then "midnight to 1am".

7. Save everything when done.

There are a whole load of services available Google Apps Script can do e.g. Calendar, Mail and Maps.

» Gmail Snooze with Apps Script | Official Gmail Blog

No comments:

Post a Comment

Do provide your constructive comment. I appreciate that.

Popular Posts