Tuesday, September 6, 2011

Gmail Snooze - updated

The Gmail Snooze script was introduced about two months ago in the Gmail Blog. I had a very similar script for mutt about 4 years ago (just before I moved to Gmail), and I was very happy to see the Gmail version coming, this is an essential part of being productive.

I extended a script a little, so that it have the following functionality also:
  • It is extended to 30 days, so that you can postpone emails up to a month. Now that the label names are sorted numerically, these label names are in the proper chronological order.
  • It allows you to star the messages which are returned to the inbox. Only the last email will be starred in each conversation.
To install the script, follow the video tutorial in the official announcement, except that you should paste the script below the video instead:


The modified script is the following:
var PAGE_SIZE = 100;  // Don't use more than 100 because thats the max you can write
var MARK_UNREAD = true;
var STAR = true;
var DAYS = 30;

function getLabelName(i) {
  return "Later/" + i + " days";
}

function setup() {
  GmailApp.createLabel("Later");
  for (var i = 1; i <= DAYS; ++i) {
    GmailApp.createLabel(getLabelName(i));
  }
}

function moveSnoozes() {
  var oldLabel, newLabel, page;
  for (var i = 1; i <= DAYS; ++i) {
    newLabel = oldLabel;
    oldLabel = GmailApp.getUserLabelByName(getLabelName(i));
    page = null;
    while(!page || page.length == PAGE_SIZE) {
      page = oldLabel.getThreads(0, PAGE_SIZE);
      if (page.length > 0) {
        if (newLabel) {
          newLabel.addToThreads(page);
        } else {
          GmailApp.moveThreadsToInbox(page);
          if (MARK_UNREAD) {
            GmailApp.markThreadsUnread(page);
          }
          if (STAR) {
            var messagesToStar = [];
            for (var i = 0; i < page.length; i++) {
              var messages = page[i].getMessages();
              if (messages.length > 0) {
                var message = messages[messages.length - 1];
                messagesToStar.push(message);
              }
            }
            GmailApp.starMessages(messagesToStar);
          }
        }     
        oldLabel.removeFromThreads(page);
      }  
    }
  }
}
In the next blog posts I will write some more about how to use this for being productive (and not just procrastinate). :)

No comments:

Post a Comment