Gmail Snooze with Apps Script

Wednesday, July 27, 2011 | 2:53 PM

Labels: ,


What can you do with a little bit of Apps Script?

At Google, we all use email very heavily-- for communicating with other Googlers, for task management, and to mail around funny pictures of kittens. Because of the volume of email we all deal with (the internet is full of kittens), a lot of Googlers subscribe to the “inbox zero” philosophy. In inbox zero you try to keep your inbox empty of all but the emails you currently need to deal with.

What is Gmail Snooze?

In managing our inboxes, one feature that we really wanted was for Gmail to let you “snooze” an email. To snooze an email means to archive it for now, but to have it automatically reappear in the inbox at some specified time in the future. With Apps Script you can extend Gmail yourself to add this functionality and a lot more.



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.


Corey Goldfeder profile

Corey is a Google software engineer on the Apps Script Project, based in New York. He has previously worked on Similar Shape search for 3DWarehouse, and as a robotics researcher before joining Google.



Updated: 7/29/2011 to add additional instructions for refreshing the Gmail window after running the setup() function

65 comments:

Waqar Ahmad said...

Wow, i like it.. a very brilliant usage of apps script

Hari said...

Does the app script run in the cloud or do you need to you have it open in a browser tab?

Olivier Dagenais said...

Corey, what clock is used for the trigger? If it's UTC, then that might shift the snoozes at different times of the day throughout the globe (might be worth mentioning), but if it's "local" time for the user, then is the "midnight to 1am" selection guaranteed to only fire once when daylight saving time changes in autumn?

Cheers,
- Oli

Daniel said...

Sweet. Can you implement these other features while you're at it: http://messymatters.com/email

Anonymous said...

Can you guys please look into the spinning icon in my browser tab. It keeps spinning, Gmail never loads completely.

dcsillag said...

Olivier, you can set the time trigger for whatever time you want it to run. See http://code.google.com/googleapps/appsscript/guide_events.html#TimeTriggers for details on how to set up the trigger.

Drew

Yserbius said...

Nice. Thanks Chanoch. I never thought of using scripts to hack GMail.

Yserbius said...

Thanks Chanoch! I didn't realize that you can run App Scripts in GMail. And congratulations on being quoted in Lifehacker.

HockeyPhool said...

I'm able to create the script, but when I run the "setup" function I don't see any evidence that it's created the labels. I did authorize the script to modify my Gmail account.

Albin Stööp said...

Really cool thought behind the actual script. I love the function, it's actually a pretty necessary function.

Anonymous said...

the function is brilliant. i've always wanted an email tickler. THANKS!

Michael said...

@Hari-Once you have told the message how many days to snooze it runs in the cloud and you do not need to have a browser tab open nor even have your computer on.
At setup you will create a once a day, time driven, trigger that resides on Googles computers(in the cloud) and will automatically update the snooze period on Googles computers.

Michael said...

@Olivier Dagenais
Your local time zone is set up in the properties of the script editor when you create or upload the script. At setup you will also create a trigger from the script editor where you will specify what time of day or night you want the update snooze script to run.

Anonymous said...

I ran the script but dont see the labels in Gmail...any ideas?

Anonymous said...

I have followed the instructions, the script was enabled, but no labels were created in Gmail, even after reloading it completely.

If/when this will work, I can't wait to try to expand this, or make new great scripts :-)

Saurabh Gupta said...

After you run the setup() function, you can refresh your inbox and you will see the labels. All labels are create under a label called Snooze.

Please post on Apps Script forum if you still need support.

http://www.google.com/support/forum/p/apps-script/

lfobo said...

labels don't show up in Gmail, even though no error occurs during creation.

Anonymous said...

Now the labels are there!
It took about 30 minutes to appear, maybe I was too excited to wait for them to appear, lol

Reedy said...

Brilliant idea, but it doesn't work!
Ran through the install 3 times now, following it word for word, but same as other peoples comments - no labels were created, and no evidence that anything has changed...

TerryKarges said...

I don't see the "setup" under the "Select a function to run" dropdown...am I doing something wrong?

Michael said...

A couple of other sites with instructions that might help those having problems.

http://questioningtechnologyzerzan.blogmonster.de/2011/07/29/how-to-add-a-snooze-button-to-gmail-no-extensions-required-video/

http://www.ditii.com/2011/07/27/creating-gmail-snooze-extension-with-google-apps-script-code/

Ryan Boyd said...

NOTICE TO ALL -

Definitely see Saurabh's comment above. Are you refreshing your Gmail window after running the setup() method?

Also, support can be handled much more easily via the forum. Please use that to get help with problems:
http://www.google.com/support/forum/p/apps-script/

Cheers,
-Ryan

lfobo said...

it now worked for me. It is necessary to close all (!) running instances of gmail. I presume that leaving one gmail-tab open let the setup script fail in my case.

deeps said...

awesome!! works for me

Phil Simon said...

Like others, I followed the directions and the labels were not created. I'd love to find out when this works.

I'm running Chrome on Apple OSX 10.6

Nathan Sudds said...

I'd recommend using Boomerang a Firefox/Chrome addon that does this and a lot more effortlessly --- 1 click install :)

www.boomeranggmail.com

Oscar said...

I install last night but it not worked. How can I do now?

Oscar said...

I installed last night but it not worked.
What can I do?

VinceL said...

Nice work. I too was stymied for a moment when the labels didn't seem to appear. But having closed Gmail and re-run the setup. Everything looks right.

In Outlook-land, I usually just build a calendar appointment and drag my e-mail item into it, in order to get it out of my inbox (I'm an inbox zero fan, as well). But, that's not as convenient to do with Gmail/Gcalendar. This is a good solution!

Thibaut said...

How do we disable that stuff ?

I installed it. I guess it worked since i had the new label.
Though, since i installed it, i have problems with gmail widgets on my android phone.

So, I want to remove that script now. But i don't know how i can do that. Don't know where the script is now.

Could anybody help me please ?

Kenzo said...

Pretty nice, altough it makes the menu a bit too long. Now that I've tried it, I would like to unisnstall it. How can I do ?(I've deleted the project, but apparently it wasn't enough)

Kenzo said...

A very nice feature, but now that I've tried it I would like to uninstall it. How do I do it?( I've tried to delete the project, but apparently it wasn't enough)

DM said...

Instead of creating loads of labels, why not use the midnight event to add email IDs to the spreadsheet and then track the snooze progress in the spreadsheet each night. It would be a bit more complicated, but a lot neater and could be a nice example to build on.

Peter Barganski said...

My Labels never got created and this morning I get the following error emailed to me:

Your script, Gmail Snooze, has recently failed to finish successfully. A summary of the failure(s) is shown below. To configure the triggers for this script, or change your setting for receiving future failure notifications, click here.

Details:

Start Function Error Message Trigger End
7/31/11 12:31 AM moveSnoozes TypeError: Cannot call method "getThreads" of null. (line 27) time-based 7/31/11 12:31 AM
Sincerely,

Google Apps Script

Need help? Visit the Google Apps Script documentation. Please do not reply to this message. (c) 2010 Google

Regards,

Peter

mukteer.gaan said...

my 2 query

1. can i use it for any gmail account
2.i cannot find the stup button

please remember I am using chrome browser and I have 1474 filters to use

DM said...

I have just written a spreadsheet based timer which allows you to set arbitrary dates in the future and customize the actions performed on expiration of the timer. I have applied to publish the code, but in the meantime you can view it here here.

Anonsage said...

This is awesome! I've never used App Scripts before, but now I'm definitely planning a few more projects this summer with it. =D

Mark said...

Start Function Error Message Trigger End
7/30/11 12:10 AM moveSnoozes TypeError: Cannot call method "getThreads" of null. (line 28) time-based 7/30/11 12:10 AM

Alexander Nikolaev said...

Not working for me either. No labels created after running setup() and

TypeError: Cannot call method "getThreads" of null. (line 27)

error in my inbox.

Saurabh Gupta said...

Please request support in our forum. Our community is very active.

http://www.google.com/support/forum/p/apps-script/

thank you
Saurabh

Anonymous said...

My own implementation of snooze email reminders for to-do tasks using google apps script:
http://www.antoniorinaldi.it/build-your-own-pestering-email-reminder-using-google-docs/

Peter Barganski said...

NO LABELS SOLVED!
Ok, so you have to actually save the Spreadsheet, and FIRST then Save the Script and run the Setup Trigger!

If you're like me, I had lots of triggers now so it Script Editior, go to Triggers -> All you triggers and delete all the old ones and recreate just the one.

Thoughts of the Enigma said...

It works! But now I cannot find my script, I did not save the spreadsheet, I saved only the code and the trigger. Where can I find the script.

Peter Barganski said...

The script is gonne - since t wasn't saved!

All that remains are the "triggers" which seem to be just like a schedule which points to a non existent script.

Create the spreadsheet and save it. Open script editor, cut and paste and save the script. I saved the spreadsheet at this stage too - making sure that the new script is save in it.

Then rerun setup trigger and you're all set!

Very strafe way of doing this spreadsheet/script dependancy - Google Developers - can this be recoded please.

DM said...

I agree with Peter Barganski: the current relationship between spreadsheets and scripts is very odd.

Could you not elevate scripts to the level of "product", i.e. on equal terms with gmail, calender, docs, etc. Or at least on equal terms with docs-spreadsheet, docs-writer, and docs-presentations, etc.

Obviously this is quite a big change, but scripts have great potential and deserve their own fully-featured home!

Gecoote said...

Hello,

I guess the instruction "oldLabel.getThreads(0, 100);" should be "oldLabel.getThreads(n, 100);" where you add n at each loop.
If not you are always looking to the first 100 items and it's never finishing.

Best regards

Corey said...

Nope, the code is correct. The script moves all the threads in the "page" out of the oldLabel, so by the time you get to the next iteration of the loop, threads 0 to 100 are the next 100 threads. If you change it as you did, it will fail on threads 100-200 if you have that many snoozed.

Badrumsrenovering said...

Does this awp work for ANY gmail acc?

And where can I set up this file?

Markus said...

I try to install this last night but it didnt work so well.

What should I do know?

Thanks

Mike Kakuk said...

mine is not working either....how do I delete and start over?

Mroka said...

I cant get all the ones that say it dosent work and etc. Mine is working, fully.

Thanks anyway. Gmail rocks!

Carey said...

I must have done something wrong, because I can't find the label in Gmail, and I get an email every day that the script is not working.

How do I delete it?

Anonymous said...

You should buy boomerang and then it could be integrated tighter into gmail. And free. That would be awesome. Boomerang has a really nice, functional UI.

Anonymous said...

Is there a way to set a filter for incoming messages to trigger a snooze to jump to the inbox? This might be a separate thing, unless there's a way to add it directly. Like if I want to snooze a message only until a different anticipated message arrives, could that happen. Only show this in inbox when message from z arrives?

Badrumsrenovering said...

Works for me. Thanks :)

Gå ner i vikt said...

Works great for me :)

Rabatt Kampanjer said...

Well written and nice pictures. And the numbers are just growing every day... :)

Ryan Boyd said...

@Rabatt - "Select All" + "Archive all" :)

commet said...

I would love to find out how to automate the install to install an a user's gmail. Ofcourse, with the user's permission, but all the user should have to do is click on a link I have emailed them to run the Snoozes install.

Thanks Much JS

My Piano's Muse said...

For everyone who had the following error:

"TypeError: Cannot call method "getThreads" of null. (line 29)"

I found one thing to check for that might fix it. In your script window, go to View>Execution Summary

It will tell you what tasks the script completed before crashing.

In my case, I noticed that it was stopping after 5. When I debugged it was getting null with a value of 5. It turns out, I had actually deleted the label for the 5th day. So when the script asks Gmail to pull up the 5th day label, instead of finding zero threads, it was finding null threads and crashing the script.

I created this 5th day label, and this script now works. Oops.

My Piano's Muse said...

I was getting this error:

TypeError: Cannot call method "getThreads" of null. (line 29)

In my case, it turns out I had accidentally deleted the day 5 label. That's why, instead of pulling up zero threads, Gmail was pulling up null threads. It couldn't find the label.

Tim said...

Could you please update this blog posting with how to remove the script for those of us who applied it? Something in the google Drive transition has deleted the script, yet still leaves us with daily messages like this one:

Date: Tue, 10 Jul 2012 17:30:18 -0700 (PDT)
Message-ID:
Subject: Summary of failures for Google Apps Script: Not found
From: apps-scripts-notifications@google.com

Your script, Not found, has recently failed to finish successfully. A
summary of the failure(s) is shown below. To configure the triggers for
this script, or change your setting for receiving future failure
notifications, click here.
Details:


Start Function Error Message Trigger End

7/10/12 8:11 AM moveSnoozes We're sorry, a server error occurred. Please
wait a bit and try again. time-based 7/10/12 8:11 AM


Sincerely,
Google Apps Script

Need help? Visit the Google Apps Script documentation. Please do not reply
to this message. (c) 2012 Google

Richard Morrison said...

Great idea, Google Scripts is going to be very useful.

I had another idea.

Use filters together with Gmail's "disposable" address feature to enable you to email yourself in the future.

E.g. you+3days@gmail.com

gmailsnooze said...

I took this script a little further and wrote a new one, which creates labels for the following 2 (or more) months, that you can snooze your mails. Take a look at it:

Gmail Snooze

Diane Evans said...

I had high hopes for this, to effectively replicate "flag for follow up" from Outlook.

Sadly the first part of the description talks about messages, but it's actually threads which are being 'moved', given the APIs for assigning labels can only be applied to threads, and not messages.

I logged in this morning to 32 unread emails, for two actual messages I'd marked to snooze a couple of days back. It had picked up all the emails in the thread and applied accordingly.


So sadly this doesn't work for me.