I want to improve a macro that I regularly use, which performs OCR on an image and converts it into single-line text.
When I copy code, I’d like to preserve the exact formatting of the code: line breaks and tabulations.
What I’d like the macro to do is:
1. Copy the image from the selected area to the clipboard
2. Perform OCR and place the text with preserved line breaks and tabulations in the clipboard
3. Perform OCR again but this time place a single-line version of the text in the clipboard
That way, I’d have 3 items in the clipboard history.
Unfortunately, the macro I currently have doesn’t work: I can’t manage to get 3 separate copies into the clipboard.
Firstly, if you don't use the fancy features of screencapture (like selecting windows), (which usually don't make sense for OCR selection), you can use the native Prompt for Screen Rectangle action.
That avoids having to muck with checking the Escape key.
On the Mac, line breaks in the clipboard may be either Return or LineFeed (or rarely, Return LineFeed). To join the lines, replace regex \R with space.
Thank you for this. I will change to the native Prompt for Screen Rectangle.
But it doesn't solve the first "copy to clipboard" trick. In the %Var% variable, the text has line breaks but no tabulation, which is bad for coding.
And I don't know why this Macro doesn't keep the two "copy to clipboard". At the end, the clipboard history has the screen capture and the last variable %VarLine%, but not %Var%
I have not found any sources to back up or contradict my thoughts about this but… I would expect tabulation to be a challenge for OCR, since that would involve optical absence-of-character recognition! It seems to me that the algorithm would need to be able to reliably work out the relative length of spaces, and recognise their context, and somehow know whether two or four or another number of estimated spaces indicated a tab, and whether such equivalence were valid or significant based upon what it detected the programming language to be...
So if I am thinking along the right lines there (and not overthinking it) that seems like a challenge for specialist AI analysis rather than regular OCR.
Based on the preceding posts, here is my rendering of the macro, to, at least, aid my understanding! It seems to work fine and maybe it will add another perspective.
Beyond step 1, the macro does not use variables, since, as far as I know, access to them is not needed outside the macro, and they did not aid clarity.
If you do want to use variables, and they are only for use within the macro, it would be neater to make them local variables (e.g. “local var”). Then they will only exist while the macro is running.
Thank you for your answer. I don't know why, but it's ok with your Macro, I've got 3 new entries in the clipboard : the image, the line breaks text and the one-line text !
And you are right, I will look if it's possible to launch a little bit of IA within KB to make it. I don't know if it is possible but I will search in the forum.
Oh, you mean AI within KM, yes? Well maybe, or maybe a solution based on something else is possible.
A quick Web search brought up items such as the following, which relates not to Apple‘s OCR, but to Tesseract. I‘m not sure that that engine is always available under KM; I think it might depend upon MacOS version but perhaps someone could kindly correct/update us on that..?