Google Apps Scripts and You

August 13, 2015

Whenever you're working with automated systems you're almost certainly getting a deluge of daily emails. Gmail's filters can be quite powerful for extracting a signal from this noise, but sometimes it's not enough. For instance, you ran a rolling-restart over your fleet of machines and got an email from each one notifying you about the event. Usually it's important that you know when a machine might be flapping or something, but now you have over 400 emails you want to "mark as read".

Here's where Google Apps Scripts comes in. Google Apps Scripts lets you write some server-side javascript to interface with anything in your apps account.

It's as easy as this:

function markOldEmailsUnderLabelAsRead() {
  var labels = [
    // Substitute the labels you want to mark as read here...
    "server-messages-cron",
    "server-messages-reboot",
    "server-messages-django",
  ];
  for (var i = 0; i < labels.length; i++) {
    var labelName = labels[i];
    var threads = GmailApp.search("label:"+labelName+" is:unread older_than:2d");
    GmailApp.markThreadsRead(threads);
  }
}

Here's how we deploy it to run on your inbox:

  1. Navigate to Google Apps Scripts to start composing a new script.
  2. Select "Create Script for Blank Project"
  3. Paste the code at the beginning of this list into the editor
  4. Save the project
  5. Authorize the script to run and test against your email
  6. Add a trigger for the script to run on under: Resource > Current Project's Triggers
  7. Done