Gmail OCR with 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() {

// Initialize the Script
function StartProgram() {
  var DRIVE_FOLDER = "Gmail Images";  
  var GMAIL_LABEL  = "Processed";


// 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) {


// If the Gmail label is unavailable, create one.
function createGmailLabel(name) {
  if ( ! GmailApp.getUserLabelByName(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 =;
    if (folder.getName() === name) {
      found = true;
  if ( ! found ) {
  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 ="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