“Execute a Shell Script” + Async JavaScript Function


I have a Node.js file that uses an API to retrieve & download images via an async function that returns a promise.

When I run the Node.js file from MacOS's Terminal app, it waits for my async function to finish downloading the images before exiting. Within Keyboard Maestro however, when I run the Node.js file from the “Execute a Shell Script” action or using the command “do shell script” within the “Execute an AppleScript” action, the script closes almost immediately with no error messages, simply returning an empty string “”.

Here is the command I am running within the “Execute a Shell Script” action or the “Execute an AppleScript” action (using the command “do shell script "…"”)

/usr/local/bin/node ~/file.js

Within Keyboard Maestro, how can I make the shell script wait for the async function to finish like it does when run from the Terminal app?

Note: if I include the $PATH variable from the Terminal app into Keyboard Maestro, the same issue occurs. So I'm not clear on what's causing the behaviour difference.

Kind regards,

I've got this to work.

I had to export all my environment variables inside Terminal into Keyboard Maestro. I did this by running the command “env” on Terminal and copying and pasting the output inside Keyboard Maestro's “Execute a Shell Script” action (at the top).

For example, if “env” on Terminal outputted:


Then I needed to write my script in Keyboard Maestro's “Execute a Shell Script” as:


/usr/local/bin/node ~/file.js

But doing this for every relevant environment variable you find.

Note: if you define PATH correctly you don't need to prepend your program calls with /usr/local/bin/ but can just write node, for example.

export PATH="/usr/local/bin/"

node ~/file.js

Hope that helps anyone else!

As a suggestion: What I do to keep a 'single source of truth' for my ENV (instead of having to edit them in different 'Execute a Shell Script's) is starting my "Execute a Shell Script"s with:

source ~/.bash_profile 

That will load the .bash_profile within the (bash) shell.


Thank you for the great suggestion!