Can I lower the priority of a KM macro?

In macOS it's possible to use the "nice" command to lower the priority of a task. Can I do something similar with a KM macro, or perhaps with a single action in a KM macro?

I do a lot of OCR and Find Image, but sometimes they bog down the responsiveness of the macOS GUI, and I would prefer high responsiveness over the speed of these tasks.

Alternatively, is there some other macOS command line utility (or even a third party utility) which can perform the same "Apple OCR", (like there is with "screencapture") because if there is, I can use the "nice" command to get the OCR to work at a lower priority. Hmm, I just googled it, and there's something called OWL OCR which might fit the bill. (It's on the Apple App store, and advertises itself as a GUI app, but comes with a command line version.) I just tried it out. It can read either from a file or directly from the screen, but if you read from the screen, it has to be the whole screen and not some portion of it. That's helpful, for many situations, but not perfect.

Alternatively, does AppleScript allow us to run something in a lower priority? If so, maybe I could run a KM macro from AppleScript with a lower priority. I tried googling this idea, but it was difficult.

May I make a feature request that the Execute Macro action have a new feature that lets us "lower the priority" of the macro when it runs? This would also solve my problem. It doesn't sound too difficult to implement. :slight_smile: (as anyone would say for any feature request)

For those who want to know, here are the options for the OWL OCR command line app: (it's free for 3 weeks, then costs just $10 for a lifetime license, although I'm not sure if the command line will work in its free mode.)

OwlOCR Command Line Interface
---------------------------------

Inputs:
------

- Use with files --input /path/to/filename.jpg or screenshot using --screenshot.
- Screenshots require screen recording permissions to the terminal application or other application where you are launching OwlOCR CLI from.

Output:
------

- OCR results are printed to stdout, unless called with --silent.
- To export a PDF file use --output /path/to/newPdfFile.pdf.

Example 1, capture area of screen and get text to stdout:
--------------------------------------------------------

/Applications/OwlOCR.app/Contents/MacOS/OwlOCR --cli --screenshot

Example 2, convert image file to searchable PDF:
-----------------------------------------------

/Applications/OwlOCR.app/Contents/MacOS/OwlOCR --cli --input myimage.jpg --output mypdf.pdf --silent

Notes:
------

Full list of supported parameters are: --cli --screenshot --input --i --output --o --silent --force --silence-notifications --show-dock

--cli is always required to start command line operation. It is a safety precaution for the GUI users to not inadvertently open CLI.

Bugs, feature requests, or support requests -> support@owlocr.com.

It occurred to me that you could run the task only when the CPU usage is below the percentage of your choice.

I learn from a thread on Stack Overflow that the current cpu usage value can be obtained with the following shell command:

ps -A -o %cpu | awk '{s+=$1} END {print s "%"}'

That's an amazing idea, and for reference I put it into a macro like this:

But I can't say that this meets my needs because I still want the macro to run when my CPU load is high, just at a lower priority. Based on the last 5 minutes of looking, my CPU runs at about 150% on average, which doesn't even make sense, since 100% should be the maximum.

1 Like

With multiple cores, the maximum CPU should be the number of cores in your Mac x 100, if I’m remembering it right.

-rob.

1 Like

The OCR cannot be “niced”. You will have to adjust your macro to do less frequent OCRing.

For example, instead of Pause Until action, you might use a While action with a Pause action to slow it down.

1 Like

Quick question: When you say "The OCR" do you mean the OCR in the KM actions, or are you referring to any third party app that uses Apple's OCR?

I mean any of Keyboard Maestro’s CPU intensive facilities, like OCR or Find Image (and to a degree also things that involve interprocess communication like AppleScript and JavaScript), will take CPU and/or potentially affect performance when they are being performed, and if you want to have less impact on your Mac, use the facility less.

In that case, rather than setting a threshold for when the macro can run, have it run less often as CPU usage increases. The macro (or actions) could run again when n seconds have elapsed, with the calculation for n involving multiplication by the inverse of the CPU usage figure (or think of it as a divisor if you prefer).

Good idea. I'm going to mark that as a solution, especially when combined with your first comment. I can code that idea.

However, I'm also going to tinker with the command line version of OwlOCR which, as an external command, will allow men to launch it with "nice" (to lower its priority) which may also have a positive impact on system responsiveness. If it works, I'll report it here.

1 Like