“Execute a Shell Script” + Async JavaScript Function

Hi,

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,
Chris

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:

API_CREDENTIALS=Foo

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

export API_CREDENTIALS="Foo"

/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 API_CREDENTIALS="Foo"
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:

#!/bin/bash
source ~/.bash_profile 

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

2 Likes

Thank you for the great suggestion!