Gmail OCR with Google Apps Script

G
Published in: Gmail - Google Apps Script

The script will save Gmail attachments to Google Drive and because Drive supports OCR for images, you can then search for text inside these images.

/*

    Auto-Save your Gmail Image Attachments to Google Drive
    ======================================================

    Written by Amit Agarwal on 05/28/2013

    To get started, choose Run -> Authorize and grant the
    necessary permissions. Then choose Run -> StartProgram.

    The default Google Drive folder for saving the image
    attachments is "Gmail Images" and once the message has
    been processed, Gmail applies the label "Processed" to
    that message. You can change the defaults in line 26 & 26.

*/


// Authorize the Google Apps Script
function Authorize() {
  StartProgram();
}

// Initialize the Script
function StartProgram() {

  var DRIVE_FOLDER = "Gmail Images";
  var GMAIL_LABEL  = "Processed";

  createGmailLabel(GMAIL_LABEL);
  createDriveFolder(DRIVE_FOLDER);
  createTrigger();

}

// The script will check your Gmail mailbox every minute
// with the help of a CLOCK based trigger.
function createTrigger() {

  var triggers = ScriptApp.getScriptTriggers();

  for(var i in triggers) {
    ScriptApp.deleteTrigger(triggers[i]);
  }

  ScriptApp.newTrigger('saveGmailAttachments')
   .timeBased()
   .everyMinutes(1)
   .create();

}

// If the Gmail label is unavailable, create one.
function createGmailLabel(name) {

  if ( ! GmailApp.getUserLabelByName(name) ) {
    GmailApp.createLabel(name);
  }

  ScriptProperties.setProperty("LABEL", name);
}


// If the Google Drive folder is not present, create one.
function createDriveFolder(name) {

  var folders = DriveApp.getFolders();
  var folder, found = false;

  while (folders.hasNext()) {
    folder = folders.next();
    if (folder.getName() === name) {
      found = true;
      break;
    }
  }

  if ( ! found ) {
    DriveApp.createFolder(name);
  }

  ScriptProperties.setProperty("FOLDER_ID", folder.getId());
}

// This will auto-save the image attachments from Gmail to Google Drive
function saveGmailAttachments() {

  var label_name = ScriptProperties.getProperty("LABEL");
  var label = GmailApp.getUserLabelByName(label_name);

  var folderID  = DriveApp.getFolderById(ScriptProperties.getProperty("FOLDER_ID"));

  // Scan for threads that have image attachments
  var threads = GmailApp.search("in:all -in:spam -in:trash -in:" + label_name
              + " has:attachment filename:jpg OR filename:png OR filename:gif", 0, 10);

  try {

    for (var x=0; x<threads.length; x++) {

      var messages = threads[x].getMessages();

      for (var y=0; y<messages.length; y++) {

        var attachments = messages[y].getAttachments();

        for (var z=0; z<attachments.length; z++) {

          var file = attachments[z];

          // Only save image attachments that have the MIME type as image.
          if (file.getContentType().match(/image/gi)) {
            folderID.createFile(file);
          }
        }
      }
      // Process messages are labelled to skip them in the next iteration.
      threads[x].addLabel(label);
    }
  }
  catch (e) {
    Logger.log(e.toString());
  }
}
📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Gmail - Google Apps Script

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
A
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account
G

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube