Google Apps Script

Total overkill for a minor Gmailify problem

Google Apps Script logo My primary email address is a Gmail address, but I have an old Yahoo email address that still gets the occasional email. Rather than check both accounts, I use a Gmail feature called Gmailify that lets me use Gmail like an email client to check another service’s mailbox.

I open up Gmail and, once in a while, a Yahoo email is mixed in with the rest of my emails. I tried to create a filter that would apply a Yahoo label, but one downside to Gmailify is that filters are not applied to Gmailified emails. Google Apps Script to the rescue.

Google Apps Script

Google Apps Script is a powerful platform that allows you to build javascript applications that can interact with Google Workspace (i.e. Gmail and Google Docs). I unfortunately don’t know much javascript, but a few Google searches gave me two scripts I could mash together.

The first is to apply a label to the first three threads in a Gmail inbox.

var label = GmailApp.getUserLabelByName("MyLabel");
var threads = GmailApp.getInboxThreads(0,3);

The second is how to search for particular emails.

var threads ='is:starred subject:"IMPORTANT"');

Putting it all together

Combining the two parts, I put together a script that did what I needed

function labelYahoo() {
  // Get the appropriate label
  var label = GmailApp.getUserLabelByName("Yahoo");
  // Find the appropriate emails
  var threads ='to:(');
  // Apply the label to all of the emails found in the query

I pasted the above code into a new project on the Apps Script site and did a test run by clicking the run button. I gave the app permissions to my Gmail account, and my Yahoo emails were successfully labeled.

Adding a trigger

The last step was to run the function periodically so I wouldn’t have to push the run button every time. There’s no “whenever I get a new email trigger,” so I settled for a time based trigger. This isn’t mission critical for me, so I set the function to run every hour.

Google Apps Script trigger modal

I’m barely scratching the surface of what Apps Script can do, but I’m happy with what I built. If you’re interested in other things you can do, Google has a GitHub Repo with more examples.

See also