How do I send email when OCR fails?

I want the following macro to send me an email when a timer ends, but it’s not working. My screenshot shows what the timer looks like and the time.txt file the macro generates recording its value every minute based on the OCR action (I define the OCR area just before running the macro).

Here’s a gif of the timer as it changes. But I only OCR the portion of the screen with the changing numbers (shown as the red box). And those change format as time goes beyond seconds to minutes to hours; ie, from format ss.sss, to mm:ss.sss, to hh:mm:ss.sss

s

The macro below shows that I tried using Try/Catch and also an If/then statement to catch the error which appears when the timer ends. But it never sends me an email, even though when I test the email action alone, it works (of course, with a real email address inserted)

How can I get this to send me an email when the timer stops? By the way, the timer is from the Subtitle Edit app’s Whisper transcribe feature, which can take hours and that’s why I want to be notified when it ends.

Find when whisper transcription is done.kmmacros (9.9 KB)

What exactly you try to recognize by OCR, is it the text file? Why not read this text file directly? As always in this forum, better to describe the process and your needs than asking why something doesn’t work.

Agree with @nutilius.

I am not at a computer so cannot test your macro but note the following.

a) You are capturing the entire area rather than the last row of the OCR area. Is this what you want to do?

b) Your if condition is not correct; per the Wiki your test is looking for numbers.

You would likely be better off using “does not contain” with a string or “does not match” with a RegEx.

c) Test each action or use the debugger to see where you are going offside.

I hope this helps.

1 Like

@nutilius @Joel

As I wrote the .txt file is generated by my macro, specifically in the “Append Text to File” action. That text in turn is generated by the OCR action which captures the portion of the screen where the timer is changing.

In other words, I have an app that shows a timer and I want to have an email sent to me when that timer is finished since it takes hours. The elapsed time is recorded every minute in the .txt file I generate, so I when I come back to the computer after it finishes I can see from the last entry when it finished. But I want it to email me when it finishes so I know when to come back to the computer.

I hope that’s better. I’m trying to be as clear as I can.

You are capturing the entire area rather than the last row of the OCR area?

No, I’m only capturing the area shown by the red box in my diagram; ie, just the changing time

It's ridiculously early here (love those airport runs!), but couldn't you simplify this:

You're already recording (some value) to a text file every minute. Assuming that value always changes within that minute if the OCR is still happening, why not use two variables instead:

Set old_timer to 0
start repeat loop that does stuff that generates timer values
  set new_timer to value from thing
  if new_timer = old_timer
    comment: Timer hasn't changed, so job is done
    send email
    exit loop
  else
    set old_timer to new_timer
repeat

Wouldn't that do what you want it to do? Again, apologies for any gross logic errors, as I shouldn't be awake right now :).

-rob.

1 Like

@griffman extra points for half-awake answers

At the bottom of my text.txt file image, it shows what happens when the timer stops: an OCR error “Warning: Invalid resolution 0 dpi. Using 70 instead.” I’m assuming that’s what happens when it sees pure white rather than text?

In any event, the timer disappears so you don’t have the same value written over and over to the txt file. That .txt file is just a record of what the OCR action gathered.

I suppose I could test for that warning text, but I don’t really understand it and figured there must be a better way

That'd be even simpler then—just check for that text string, which if I'm reading things right, would be in local_time, wouldn't it?

-rob.

@griffman

I’ve tried using if local_time “is empty” in my If All Conditions met action but I still didn’t get an email. I double check and I do get an email if I use gear>Try Action

regarding RexEx, I’m not that good at it but am working on getting the correct string right now

I’ve marked Joel’s answer as the solution because he suggested using Regex. Here’s what I used that worked to send me an email for the first time:

Now I’m receiving emails every minute — but that’s better than nothing

Agreed.

I generally start by asking AI and then test / tweak using https://regexr.com

I hope this helps!

1 Like

You’re right – life is too short to waste time with regex creation. chatGPT gave me this:

^(?:(?:\d{1,2}.)?\d{1,2}:)?\d{1,2}.\d{3}$

Also, inserted a cancel action after the email action and now I only get one email

Thanks again

A pleasure, great that you do it working.

I'm confused: Why do you need regex? Just check if the variable contains "Warning." That value is getting written to the file, which means it must be stored in the variable, right?

-rob.

@griffman I agree with you that I could check for the word “warning” in the text file that I'm making. But I think that's relying on the screen area that I chose for capturing not having any valid text in it. again as I mentioned earlier, I'm not exactly sure about what that warning message means so this is just my best guess.

I think the weakest part of my whole approach here is that portion of the screen with the timer, is subject to being covered up by changing windows or even changing workspaces. What I like about checking the timer format with rejects is that it's a strict test that I understand rather than relying on some warning that I don't understand

I understand; I thought (based on that screenshot) that the message only appeared when it was done.

-rob.

Hmmm, why not read the resulting text file, there is then no concern about it being covered?

@Joel I'm confused by your question. Let's use the example I gave in my screenshot:

31.278
1:31.379
2:31.579
3:31.679
Warning: Invalid resolution O dpi. Using 70 instead.

The first timer value is captured by OCR, put into a variable, which is then written to the .txt file and a minute passes. Then the second timer value is captured by OCR, etc... until the OCR capture step results in an error and somehow is written to the text file. Like I've said, I don't really understand that part.

Is the variable actually holding that text? Or is it an error generated somewhere, perhaps before the variable is assigned -- after all, it's the OCR step that's causing problems, right? I see in the gear to that action that "Failure Aborts Macro" and "Notify on Failure" are checked. Maybe I'm seeing the notice? But why isn't the macro failing? The macro just kept going on and on

So, I hope you can see that given my state of uncertainty about what's going on here, it seems odd for me to rely on this error appearing in the text file in a certain way. Also, I've seen other text appear in this file when I allowed windows to cover that part of the screen and that makes me nervous because I can't predict it. On the other hand, ensuring the OCR'd text has a certain format seems pretty predictable.

hope that all makes sense

I think that exists much better way to achieve what @Tony wants :slight_smile:

Just put your videos in selected folder and setup folder action - when new set file will appear in that folder, it will trigger action which can:

  • send e-mail
  • send any other notification (iMessage, Pushover)
  • move created srt file (and original) to AlreadyProcessed filder
  • … and many many more

I’ve downloaded last version of Subtitle Edit (v5.0.0-rc4) and tested it using hazel rule (the fastest for me), which generated standard notification.

Of course it can be done with KM,S hot cuts automation and probably many other tools.

Sorry, but I prefer system solutions, don’t trust in OCR methods (it is the last way to check for me).

@Tony I at the gym now but can build something later for you.

I was responding to your comment that the window gets obscured.

I was suggesting foregoing the OCR route and reading the text file into Keyboard Maestro which is done by one action.

Once the fine is in Keyboard Maestro you can proceed using the current RegEx, a simpler RegEx as @griffman suggested on WARNING, or other way.

One idea I would add is to only read the fine when the export is fine. You can do that by using an UNTIL loop and testing the file size that way you read the file only when the text file is no longer being update and get an e-mail report.

I hope this makes this clearer and easier.

@Joel - but this is only waiting for finishing process of generation the new file (subtitles file) which will appear in selected folder when process will be finished. No generating the text wile with status, no needs to read the file with the status, just only wait the file will be created and let the operating system notify tools which observe this folder and let them work (notif, send mail and other things). It can be done even with Automator.