Project Nayuki


Batch up new messages in Gmail for better productivity

Checking and handling email can consume more time than necessary. This is especially true if messages are received and read multiple times a day every day.

To reduce the impact of email on a person’s productivity, my suggested approach is to hide the incoming new messages, delay them for a period of time, then release all the new messages. This way you process emails in batch mode, which means less time spent on switching context and more speed when focused on working on messages consecutively.

If you are a Gmail user, you can implement this idea of delaying incoming messages as described here. An overview of how to do it:

  1. Add Gmail filters to operate on incoming messages.
  2. Write code using the Google Apps Script platform.
  3. Schedule the JavaScript code to run periodically.
  4. Test the script and ensure it works properly.

Setup procedure

  1. Sign in to your Gmail account. Press the gear button, see the menu, and select “Settings”.

  2. Near the top, select the section named “Filters and Blocked Addresses”. Next press “Create a new filter”.

  3. Enter your own criteria for the filter. Usually you specify a from-address. Next press “Create filter with this search”.

  4. Set the actions for this new filter. You must tick “Apply the label:” and create a new label named “Delayed messages”. It is strongly recommended (but not required) to also tick “Skip the Inbox (Archive it)” and “Mark as read”.

  5. Navigate to your Google Drive account. Press the “New” button, see the menu, and select “Google Apps Script”.

  6. At the top of the window, give a name for your script project, such as “Gmail release delayed messages script”. Then copy and paste the following JavaScript code into the main editor pane:

    /* 
     * Gmail script to release delayed messages
     * by Project Nayuki, 2016. Public domain.
     * https://www.nayuki.io/page/batch-up-new-messages-in-gmail-for-better-productivity
     */
    
    function releaseDelayedMessages() {
      var labelObj = GmailApp.getUserLabelByName("Delayed messages");
      var count = 0;
      var threads = labelObj.getThreads();
      for (var i = 0; i < threads.length; i++) {
        var th = threads[i];
        th.moveToInbox();
        th.markUnread();
        th.removeLabel(labelObj);
        count++;
      }
      Logger.log("Released " + count + " delayed messages");
    }

    In plain English, this function looks at all the email threads that have the label “Delayed messages” (the threads can have other labels as well). For each thread, the function moves the thread to your inbox (because it was specified in your filter rule that incoming threads should initially be archived and skip your inbox), marks the thread as unread (so you are aware of the new message), and removes the “Delayed messages” label (so that it won’t be processed again).

  7. From the menu bar, select “Resources”, then “Current project’s triggers”. Press “Add new trigger”. Set the form fields appropriately, such as running once per day or once per week.

    The overall effect of the filter rule plus the periodic release of messages is that new messages that match the filter criteria (such as newsletters) will first be stashed away, then later put into your inbox as unread messages. Just like other ordinary new messages, you still need to read them and archive them manually. If the threads have other labels automatically applied, the delay-and-release logic here won’t impact it.

  8. Test-run the script by selecting “Run” from the menu. You will be shown one or more security screens to approve the script to read and manipulate your email messages. Then from the menu, select “View” and “Execution transcript”, checking for any error messages.

  9. You’re done! You can sit back and wait until the next time the script runs. Once you are satisfied, you can forget about this message-delaying system you have added, and trust that it will do the right thing in the background.

    If you want to write your own custom email-handling logic in the script, please consult the Google Apps Script API documentation for Gmail.


Notes