OCR to File Comment Possible?

Hi there! Newbie here. I have a question regarding the viability of using Keyboard Maestro for automating image ocr.

Basically my question is: Is there any way to make the app write text OCR'ed from images dropped in a watched "hot folder" into the comments attribute of the image files themselves (not as separate text files or by converting to pdf)? So all image files added to a folder would automatically be scanned and have text contents of images added to the file attribute.

My use case is that I want to export my Evernote library to markdown files searchable from the Finder (or similar "shoebox" app like Eaglefiler or KeepIt). Many of my notes in Evernore contain images which are automatically OCR'd serverside by Evernote, but this OCR is not retained when exporting the notes. So, to somehow replicate the functionality – searching notes for the content of embedded image files – I would need to have an OCR output embedded as metadata in the image files. The most obvious spot is the extended attribute Comment, which, contrary to Finder comments, are persistent across file systems and cloud services).

Would Keyboard Maestro be able to do this?

I can read from the docs that it has an OCR Image action and a Set_File_Attribute action which can write to the Comment attribute.

Thanks in advance,
Toke

I think Set File Attribute writes to Finder.

Maybe Exiftool can write to an image comment:

Thanks. I think the benefit of writing to the extended attribute Comment of the file is that this exposes the text for Spotlight search – which is what I am primarily is looking for – and it is persistent across file systems and cloud sync solutions (per this post). So if File attribute writes to Finder comment, which is saved in .ds file outside the file, this is not preferable.

Also, this is me being on deep water with (in best case) ancient scripting skills, so I am a bit scared by exiftool. I will try to see if I can make Set File Attribute work first.

Here is a quick macro I made to prove the concept.
It requires exiftool is installed. I use homebrew for this.

It process the selected files in the Finder.

OCR image and save text to comment in file Macro (v9.2)

OCR image and save text to comment in file.kmmacros (2.6 KB)

1 Like

Was going to suggest a dedicated renaming tool like Name Mangler, but @JimmyHartington seems to have nailed how to add a generic comment

In the Install page there's a DMG that installs it automatically and adds it to path, so you only need to
1.- Install
2.- Add ENV_PATH to KM
3.- Run Jimmy's macro

Two things:

  1. You can reference exiftool directly without changing your environment path by using /usr/local/bin/exiftool instead of exiftool in the script.

  2. Exiftool writes to the Comment tag in the Exif header of the image, not to the Finder's commend field. So Cmd-I will not show the text but using Exiftool to read the Exif header tags will.

This version of Jimmy's macro writes to the File Attribute rather than the Exif header tag:

OCR image and save text to comment in file.kmmacros (2.6 KB)

But, you know, thinking about this a bit in the shower, I thought you might have to clean up the OCR (which is not entirely reliable), so I added a Custom HTML Prompt to edit the captured text before it is put in the Comment tag:

OCR image and save text to comment in file.kmmacros (4.1 KB)

1 Like

That is correct.

There no need for an external command-line tool.
KM will work fine:

image

image

Thanks all! I appreciate your help. I will test out the suggested solutions when I get to my Mac again monday.

The last suggestion which writes to Finder comment, saves in the filesystem database, which is different from the extended attribute Comment of the file and, I guess, exif info. Finder comments has the advantage that they are readable in the info window, while the other solutions require other tools to read. But, as the latter are embedded in the file, they are less fragile and more portable. Also, they are searchable in Spotlight/Finder which is my main aim; To locate files on the basis of image content. But I’ll check it out.

Edit: glancing at @mrpasini's very elegant solution, it seems suitable for single images files added to a folder, which would be fine for watching for a few files added to a folder now and then. But for my use case I would probably need to rely on no text cleanup post-ocr, as I have a folder exported from Evernote, which contains hundreds of note. Also, a complication would be that files are not in the root folder, but scattered in subfolders, hence there would be a need for some recursive folder lookup action which filters for common file types (jpeg, png, tiff, probably). Is there such an action?

You can certainly disable the Custom HTML Prompt (although I'd keep it around to handle especially bad results). If you find the OCR unreliable, you might investigate sending the LocalOCR variable through a spell checker like aspell to clean it up a bit (Execute Shell Script is your friend here).

You can change the enclosing For Each Path in Finder action to the similar For Each Path in Folder which has a recursive option. You'll just have to point it to the right directory.