Setting up a script environment within KM

I'm trying to run a script that works fine in Terminal, but not when executing as a shell script in KM. Normally my scripts work with various libraries just fine - so long as I include some sources:

Test ImageMagick From KM.kmmacros (2.3 KB)

(Keyboard Maestro 8.2.4 “Test ImageMagick From KM” Macro)

Unfortunately I'm getting this error:

Note: I get the same version of ruby when running in Terminal.

What am I not seeing?

My guess is that Keyboard Maestro is not getting to see the same $PATH that you are using in your $SHELL.

Add an "echo $PATH" to the "Execute Shell Script" block to see what it sees.

Is your $PATH set in either ~/.profile or ~/.bash_profile ?

(I set mine in ~/.path and source that for my login shell and for Keyboard Maestro, so I know I'm getting the right settings.)

OS X no longer has a reliable way of setting the $PATH for all GUI apps, ever since they removed the functionality from launchd several versions ago. Apple has yet to replace it with anything as easy or reliable, unfortunately.

1 Like

Presumably there is some environment variable not being set the same way after souring the various scripts as is set in the Terminal. I don't know about your scripts, but my .bashrc starts with the code:

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;

So sourcing it would do nothing.

Really what you want to do is run the env command in the Terminal and create an Execute Shell Script action and run the env command and display the results, and then compare the two looking at the differences.

Use this action:

Execute a Shell Script.kmactions (746 B)

while read var; do unset $var; done < <(awk 'BEGIN{for(v in ENVIRON) print v}' | grep KMVAR)

To get the environment variables Keyboard Maestro scripts have (with the Keyboard Maestro variable environment variables removed). Compare that with the output of env from the Terminal. Look specifically at variables PATH, variables containing the word PATH or LIB or the like, and any variables related to your scripting language (ruby in this case).

For example, my Terminal environment includes the variables:


And my PATH is different as well.

And they will affect how perl behaves if I run a perl script in the Terminal as compared to a perl script in Keyboard Maestro. If I want them to behave the same, I would either:

  • set the environment variables in the script before running perl
  • find where they are set in my Terminal bash startup scripts, move them out to a separate .setup_perl script, and source them from both the Terminal bash startup scripts and the Execute Script action in Keyboard Maestro.
  • set the corresponding ENV_ variable in Keyboard Maestro’s variables (eg ENV_PERL5LIB) to match.
1 Like

Yep, it was my PATH - for whatever reason it didn't include /usr/local/bin, which of course has tesseract in it. I added an export PATH=$PATH:/usr/local/bin line to my ~/.bash_profile and it worked without having to change my Execute Shell Script sourcing. Thanks to both of you who helped me out.