Running Shell script inside KM does not work

Hi,

I'm having trouble getting a shell script to run inside KM.
I'm even using the full path but still get the message

"env: node: No such file or directory"

So when I run this in terminal, it works:

/usr/local/bin/pushover "test"

(it also works without the full path)

Running "env" in terminal lists:

PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

Running a simple Macro "echo $PATH" in KM gives me:

/usr/bin:/bin:/usr/sbin:/sbin

I'm a little stuck here :frowning:

Best,
Jan

1 Like

the tl;dr version is that running sh from a scripting context does not give you access to the same bash environment as that which runs in Terminal.app

You will need to explicitly export any PATH or locale settings which your script depends on.

See, for example, this discussion:

( The same principles apply, for what it's worth, when you use do shell script from Applescript )

Hi Jan

I have good succes with prefixing my shell script in KM with:

export PATH=/usr/local/bin:$PATH;

So I have a command for Ghostscript, that looks like this:

export PATH=/usr/local/bin:$PATH;
gs -dNOPAUSE -sDEVICE=jpeg -dFirstPage=1 -dLastPage=237 -sOutputFile=/tmp/compare/CompareA_%d.jpg -dJPEGQ=100 -r$KMVAR_CompareResx$KMVAR_CompareRes -q /tmp/compare/CompareA.pdf -c quit

This works for me, so I do not need to specify the path to each executable.

I install most of my cli-tools with HomeBrew and they are often saved under version number. So before I knew this trick I had to update KM-macros each time there was an update.

1 Like

Note that new in 7.0, you can set the variable “ENV_PATH” to “/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin” (or whatever) and Keyboard Maestro will set the PATH environment variable to that value when it runs any script. Same for “ENV_Anything”.

3 Likes

Using this approach unfortunately resulted in this error message:

"Error: Missing required field: token"

I'm not sure if I did this correctly. This is what I entered (screenshot)

The error message here is:

/var/folders/s8/mcbvry1j60j6vy6y954g8mp00000gn/T/Keyboard-Maestro-Script-1B3A47F7-88FD-48D7-8EAE-8F974F43D2CE: line 1: pushover: command not found

The variable needs to be named “ENV_PATH” (ie, ENV<underscore>PATH). I’ve used spaces when documenting this elsewhere but that is unintentional and wrong, but needs to be an underscore.

1 Like

Ok. I think I've done it properly. It still does not work. Same message:

"Error: Missing required field: token"

Did I miss something?
How can I echo the values of global variables inside KM scripts? I tried to simply
echo %VARIABLE%ENV_PUSHOVER_TOKEN%
and
echo %VARIABLE%PUSHOVER_TOKEN%
but don't get any value for those variables.

Lastly I tried to enter the values manually. Like

pushover -t XXXXX -u YYYYY "My Message" 

but still got the same frustrating

"Error: Missing required field: token" 

I'm about to give up and look for other automation / scripting solutions :frowning:

Thank you for your efforts though.

At a glance, a couple of things jump to the eye:

  1. Standard bash syntax for variables: echo "$ENV_PUSHOVER_TOKEN"
  2. ENV ⇄ EVN (there seems to be a typo in your naming of the variables in KM. The screenshot shows EVN in two places where it should show ENV
1 Like

Thank You!!
That was the problem all the way. A Typo! :-X

2 Likes

When in doubt, simply execute the command “env” and display the results in a window, and then you can see all the environment variables as the command will see them.

2 Likes