Passing %TriggerValue% to shell script


I have read through the documentation and searched the forum, but was not able to find an answer to this question. So, at the risk of asking a very basic question: can anyone help me with this macro?

This is what I want to achieve: every time I add a PDF to a specific folder, I want the CLI tool ocrmypdf to OCR it. (This works fine when I try it at the command line.)

The only thing I did, is replacing the filenames with %TriggerValue% (which should be the file path). ocrmypdf expects the same input as output file name.

Also I'm not really sure to debug, because all I get is a macOS notification with this error (which is truncated).

I want to become better at this! :slight_smile:

1 Like

If I'm not mistaken, you have to set a variable to %TriggerValue% and then use the method shown in the WIKI to access KM Variables:

1 Like

DanThomas is right, but just to be clear how this works:

Note that in a shell script you don't use % but you do need to prefix with $KMVAR_

1 Like


1 Like

Thanks for the help! I now understand how to access KM variables, but I have ran into a new issue. That works fine now. I can rm or cat the file that was added to the folder, no problem.

The error message I get is related to the command I am running:

/usr/local/bin/ocrmypdf \""$KMVAR_path\"" \""$KMVAR_path\"" -l nld --rotate-pages --deskew

When I simply echo this command and execute that output in Terminal, it works fine (the KM variables are properly filled in).

echo /usr/local/bin/ocrmypdf \""$KMVAR_path\"" \""$KMVAR_path\"" -l nld --rotate-pages --deskew

I have tried adding the #!/bin/bash shebang, but that didn't help either.

I think the problem might be that the ocrmypdf CLI tool uses another tool called tesseract behind` the screens, and can't execute that.

I don't know how to debug this situation. The only error output I see is a macOS system notification (which is truncated). It's in my original post.

Any ideas?

So there's a little confusion here, which isn't a big deal because, well, this stuff is confusing at first.

Long story short, you only need one set of "quotes" around variables and you do not want to "escape the quotes" (which is what it is called when you put \ in front of it).

So what you want is this:

/usr/local/bin/ocrmypdf "$KMVAR_path" "$KMVAR_path" -l nld --rotate-pages --deskew

Ok, so that gets us that far.

However, now I need to ask a question, because I'm not very familiar with ocrmypdf but you have "$KMVAR_path" listed there twice. Is the intention that you want to use the same filename for input as for output? That is, if you give it a file /Users/myname/Documents/form.pdf you want to OCR it and save it back to /Users/myname/Documents/form.pdf ?

Most programs can't do that. You'd usually need to save it to a different file and then rename it to the original name at the end.

Also, from a brief look at the documentation for the ocrmypdf tool, it appears that you need to use:

  1. the ocrmypdf program itself
  2. then any options like -l nld --rotate-pages --deskew
  3. Then the name of the input file
  4. Then the name of output file.

So something like this, assuming that you want to take the name of the file from whatever Keyboard Maestro has selected:

/usr/local/bin/ocrmypdf --language nld --rotate-pages --deskew "$KMVAR_path" "$HOME/Desktop/newfile-ocr.pdf"

You'll note that I used --language instead of -l this is just a personal preference that I have developed over many years. When I am writing a script, I want it to be as clear as possible to "Future Me" what I was doing, and I never want to trust that "Future Me" will remember what I was trying to do, or how any of these programs work. So I don't trust "Future Me" to remember what the short option (-l) means, which is why I use --language because it will help "Future Me" by being more clear.

(When I am just entering commands at the command line I will use the short options, but when I am writing a script I always prefer to use the longer ones.)

So… is what I have close to what you are trying to do?

Now you'll probably have noticed immediately that there's a problem with my script. Every file that is OCR'd will be saved to "$HOME/Desktop/newfile-ocr.pdf" which will only work if you do one file at a time. But we'll deal with that later, once I'm clear on what you're trying to do.

1 Like

Thanks so much, @tjluoma! However, as it turned out, the terminal command was fine, but I had to explicitly add /usr/local/bin to the default path in KM. The script works fine now.

1 Like