Ankita
Member • Jul 29, 2011
Gmail Snooze Code - Try Out The Apps Script Feature
Many times we want to check an email but we don't have the time right now. In this case, we can âsnoozeâ that 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 we can extend Gmail to add this functionality and a lot more.
[video=youtube;p2MU5IoaveA]https://www.youtube.com/watch?v=p2MU5IoaveA&feature=player_embedded[/video]
Here's how to do this:
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.
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 #-Link-Snipped-# to run âmoveSnoozesâ.
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.
Try it out & post here about how it works.
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 we can extend Gmail to add this functionality and a lot more.
[video=youtube;p2MU5IoaveA]https://www.youtube.com/watch?v=p2MU5IoaveA&feature=player_embedded[/video]
Here's how to do this:
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"); } }To move 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 #-Link-Snipped-# to run âmoveSnoozesâ.
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); } } } }Use 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.
Try it out & post here about how it works.