Automatically Forward Starred Gmail Messages to OmniFocus

applescriptemailgmailjavascriptomnifocus

Summary: I would like to create some sort of an automated process where any messages that I 'star' in Gmail (aka 'flag' when using Gmail via IMAP) will automatically be added to OmniFocus, and then "unstarred" on Gmail and moved to another specific label called "OmniFocus".

I am quite sure that this is possible with Google Scripts but I cannot figure out how to get it to work. I would like that solution because it would not require me to run anything else on my own Mac.

Also: I have an account with http://www.spootnik.net which means that I can add things to my OmniFocus Inbox by sending email to a specific email address.

My idea for a Google Script would go something like this:

  • every ¿15? minutes or so, check for flagged messages in Gmail, and if found:
  • forward to my Super Secret Spootnik Email Address
  • Add label 'OmniFocus' to message
  • unstar/unflag message in Gmail

Unfortunately I don't know to make this work in Google Scripts, so I'm wondering if someone else might know and be able to explain it to others who might want to do something similar.

Alternatively

While I would prefer Google Scripts, I do have a Mac which is usually on 24/7 anyway, so if that is the only way to do this, that would also be an option. Unfortunately I have even less of an idea how to do that. (I assume Mail.app + AppleScript?)

Best Answer

Try:

function TJ() {
  var threads = GmailApp.search('label:inbox is:starred');
  for (var h = 0; h < threads.length; h++) {
    var messages = threads[h].getMessages();
    for (var i = 0; i < messages.length; i++) {
      if (messages[i].isStarred())
      {
        Logger.log(messages[i].getSubject());
        messages[i].unstar();
        messages[i].forward("recipient1@example.com", {
          // Advanced parameters: https://developers.google.com/apps-script/reference/gmail/gmail-message#forward%28String,Object%29
          cc: "myboss@example.com",
          bcc: "mybosses-boss@example.com,vp@example.com"
          });     
      }
  }
}
}

For more information regarding Google Apps Script, see this post: http://www.johneday.com/422/time-based-gmail-filters-with-google-apps-script