Macro silently hanging with embedded shell script

I've written a shell script that will run the open source library ocrmypdf to, well, OCR PDFs. I embedded it in a Keyboard Maestro macro and it runs perfectly on my two Macs. I've given the macro and written instructions to two different people, and in both cases the macro never completes. I've added lots of verbosity to the script, and it appears to create all of the variables correctly but never successfully runs the ocrmypdf command. Since it's a silent hang, I have no idea why.

I'm attaching the macro and I'll add my written instructions as a comment on this question. If someone could tell me what I've missed that's keeping it from running, that would be awesome.

OCR PDF verbose.kmmacros (8.9 KB)

My written instructions I've sent to the people testing for me:

How to Download, Install, and Run My OCR-PDF Macro in Keyboard Maestro

Requirements:

Steps to Install

  1. If you don’t already have it, install Homebrew: Go to https://brew.sh, copy the command they tell you to copy using the little copy button. Open Terminal and hit Enter. Ignore the glop that flies by on your screen
  2. Install OCRmyPDF: In Terminal type brew install ocrmypdf. Ignore the even more voluminous pile of glop that will fly by on your screen
  3. Also in Terminal, type echo $PATH and copy the result. It will look this /opt/homebrew/bin:/opt/homebrew/sbin:/usr/local but probably longer
  4. In Keyboard Maestro, open Settings → Variables → ENV_PATH and paste in the PATH you just copied. If ENV_PATH doesn’t exist, create it by pressing plus.
  5. We need to verify that Keyboard Maestro has full disk access since it will be writing a file. Open System Settings → Privacy & Security → Full Disk Access. If you don’t see Keyboard Maestro in the list and it’s toggled on, hit the plus button at the bottom, add Keyboard Maestro, and toggle it on.
  6. Download my macro: OCR-PDF.kmmacros
  7. Opening the downloaded macro will open it in Keyboard Maestro
  8. By default, Keyboard Maestro disables downloaded macros on import as a security precaution. Right-click on the macro and select “Enable macro”
  9. Create a new folder and give it any name you like, and in the far right pane select “Do not display in menu bar” and change it to “Display in menu bar”
  10. Drag the downloaded “OCR-PDF” macro into the new folder
  11. If you already have a folder of macros set to “Display in menu bar” you can use that one instead of creating a new one.

How to Run My Keyboard Maestro Macro

To use my little macro, you simply select a PDF in any old Finder window, then select the Keyboard Maestro menu bar item icon, and my macro should be listed under the folder name you created.

I don't have the time to install and test with the brew package, but just looking at the macro, I have a couple of observations...

First, whenever you use $KMVAR, remember to protect it with quotes:

inputName="$KMVAR_inputFile"

If users have spaces in their paths, this could be the source of the problem. My other observation is more general: Why not do the variable setting in KM, leaving just a simple one liner in the shell script? You've already got the full path and filename in inputFile, so you can use the Split Path action to get the bits you need to create the output filename:

Then your entire shell script could become:

ocrmypdf --output-type pdf "$KMVAR_inputFile" "$KMVAR_local_theOutputFile"

This would probably make your troubleshooting a lot simpler, too. (Note: I haven't tested the above, so make sure I have it right, but the concept is correct.)

-rob.

Thanks, @griffman - I'm more familiar with shell scripting so my original goal was just that. Then I thought the script might have broader appeal if I crammed it into a Keyboard Maestro macro! You've given me what I need to start learning how to use Keyboard Maestro even better, so that's awesome.

I'll definitely check on the unquoted $KMVAR to see if it's the source of the problem. I'd like to say I didn't know that but the documentation clearly says to quote it!

My AppleScript notifications show that the building of the variables is working properly but something is stopping the final ocrmypdf command.

Thanks again for the ideas,
Allison

Just noticed an error in the screenshot: The two commands below the "For Each" action should be in the "execute the following actions" section of the For Each.

"Never write macros before 7am on a Saturday!"

-rob.

1 Like

Thanks so much for the help @griffman. Indeed the main problem was with not quoting my variables. I got it to work with my shell script AND doing all but the final bit with Keyboard Maestro itself. The best part is how much I learned! Here are both scripts in their final-ish forms.

OCR PDF with Shell Script.kmmacros (14.8 KB)
OCR PDF with Keyboard Maestro & Shell Script.kmmacros (17.3 KB)

1 Like

Glad to hear it's all working!

-rob.