1

I have a call (“mercury-parser” via Yarn, using the full path to the call) that converts Web pages to lines of structured text (the page used in the call describes the service):

/Users/me/.yarn/bin/mercury-parser https://postlight.com/trackchanges/mercury-goes-open-source

If I run it in Terminal (using either /bin/bash or /bin/sh on a 2013 Mac Pro, macOS 10.14.5), I get the proper result: fourteen lines of structured text from the Web page.

However, the very same call returns an error when run as a single-line, “do shell script” AppleScript:

do shell script "/Users/me/.yarn/bin/mercury-parser https://postlight.com/trackchanges/mercury-goes-open-source"

The error returned is:

error "env: node: No such file or directory" number 127

FWIW, here are both my .profile and my .bash_profile texts (the latter created solely to try and solve this quandary), that should not be necessary, given that the full path to the call is being specified, specifically to avoid such an error:

export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH"

export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"

[[ -s $HOME/.nvm/nvm.sh ]] && . $HOME/.nvm/nvm.sh  # This loads NVM

source ~/.nvm/nvm.sh

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm

What do I have to do to get the call to run as an AppleScript?

[2019.6.26 at 11:25:45PM: Edited in an attempt at clarification.]

2019.6.27 at 1:36:57PM: Per Mark’s suggestion:

Thank you, Mark; I duplicated my .[bash_]profile, renamed it to “.bashrc”, and added “bin/bash” to the call:

do shell script "bin/bash; /Users/RF/.yarn/bin/mercury-parser https://postlight.com/trackchanges/mercury-goes-open-source"

However, (after a reboot) I still get the same error:

error "env: node: No such file or directory" number 127

Thoughts?

  • 1
    .bash_profile is only run for login shells. Terminal creates login shells, but AppleScript do shell script does not. You either need to add the relevant init steps to the do shell script section, or add them to .bashrc (which runs for non-login shells). It's for reasons like this that it's common to either have .bash_profile run .bashrc (and put the init script steps in .bashrc) or vice versa. But you should rewrite it so it doesn't add things to PATH over & over. See this question. – Gordon Davisson Jun 25 at 21:23
  • I’m going to try and clarify things and remove the TIA / chit chat. Thanks for being gracious - but the site prefers no tags / signatures and you can put that all in your linked profile if needed. – bmike Jun 25 at 21:25
  • It’s a shell/env issue indeed. Nothing node specific. – John Keates Jun 27 at 8:58
  • Your path setting should be in ~/.bashrc. Running from desktop ie via automator does not go through a login shell so nothing in ~/.bash_profile is loaded. For the terminal ~/.bash_profile will be read - and that needs to source ~/.bashrc to save typing the commands twice. – Mark Jun 27 at 10:35
  • Thank you, John; that was my assumption. Should I correct the title? What do you suggest? – Richard Fairbanks Jun 27 at 20:29
2

This was solved by KniazidisR at https://macscripter.net/viewtopic.php?pid=196447#p196447

set nodePath to "/usr/local/Cellar/node/12.5.0/bin/node "
set mercuryParserPath to "/usr/local/bin/mercury-parser "
set pageURL to "https://postlight.com/trackchanges/mercury-goes-open-source"

do shell script nodePath & mercuryParserPath & pageURL
  • Answer should focus on the problem stated in the question, I‘ve removed your additional content which didn‘t directly relate to the environment/path issue at hand. – nohillside Jul 31 at 5:02
0

This is a great question and you might need to pass us (or github / pastebin) the actual script.

Barring that, you could do two things to make this a minimal, complete, verifiable question where we might be able to recommend script changes.

  1. Find out exactly which line in your script is failing by setting debugger “set -x” and posting the results / line numbers leading up to the end error.
  2. Make a second script that just runs env node or whatever needed and failing and adding that to the post above.

If you find your error, you can do things in terminal to find the full path to each call and then your script can run in limited environments and not need the terminal setup profile dot files.

which env

Would tell you to change your script to call /usr/bin/env and same for nvm and any other programs that get no such file or directory since it’s likely your script assumes things relative to the current directory or has some variables set.

0

The problem is that your environment is not being read when run from Automator.

The Bash manual for startup files says for scripts invoked non-interactively

When Bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and execute. Bash behaves as if the following command were executed:

if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi

$BASH_ENV has not been set so no file is read.

Thus in general you need to either (or a mix) of setting the complete paths to all files inside all the scripts called and the ones they call (as per Richard Fairbank's answer or setting the $PATH in the shell script.

I have not used automator much so the following are not tested.

You could start the shell explicitly and tell it to be a login shell so it reads ~/.bash_login

do shell script "/bin/bash -l /Users/me/.yarn/bin/mercury-parser https://postlight.com/trackchanges/mercury-goes-open-source"

See this SO question where you can use more than one command so you could set the $PATH or source /Users/me/.bash_profile .....

Note also from the Bash manual linked above you should really set things in ~/.bashrc and only things used interactively (like key bindings) in .bash_profile. So you would be sourcing .bashrc

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .