Finding location of shell commands on an M1

We use a lot of shell utilities installed via Homebrew and called from Applescripts.

Since the shell command run from Applescript doesn’t assume the same relative paths as for a user, many of these aren’t being run from simply calling the command:

but from their path:

But on M1 Macs, Homebrew uses completely different paths for everything, so our existing scripts fail.
We’ve actually had this off an on before with default paths for packages changing, or users having something installed some way other than Homebrew defaults. But we’ve just fixed things on a case-by-case basis.

So I thought I could use a tool like “which” or “path_helper” to find my Homebrew tools for me, and hopefully have an elegant solution that will continue to work if paths change on me again.

In terminal on an M1, either of these will return the path to a Homebrew application:

However, inside a “do shell script,” neither of those works.
Both return:

On an Intel Mac, the “path_helper” one works from a “do shell script.”

The “which” command fails in the same way from inside a “do shell script” on an Intel Mac…
On the Intel, in Terminal I can run:

and it returns:

Yet running

do shell script "/usr/bin/which redis-cli"

still fails.
On the M1, running

in Terminal just returns:

So even if I got it working on Intel, sounds like it’s not going to work on M1.

I should probably just give up and have a function that goes and looks for each Homebrew app at each known path we’ve seen it at and returns the path for wherever it found it.

But I’m very curious what’s going on here, and if there’s any way to use a tool like “which” or “path_helper” to find these tools for the generic case.

Thanks in advance for any help. I realize a lot of you won’t have M1’s to test on, but maybe you can at least help explain how this works/doesn’t work.

I noticed that Apple changed the default shell from bash (or bash emulating sh?) to zsh.

This claims that path_helper doesn’t work with zsh.

So I tried:

do shell script "/bin/bash -s <<'EOF'
eval `/usr/libexec/path_helper -s`; which redis-cli

But I got the same thing:

Looking at the man page for an older version of brew, you could try prefix. Maybe it will work similarly on an M1.

[format]$ brew --prefix
→ /usr/local[/format]

From the man page: Display Homebrew ́s install path. Default: /usr/local on macOS and /home/linuxbrew/.linuxbrew on Linux

If you add the formula then you should get the equivalent of ‘which’.

[format]% brew --prefix xdelta
→ /usr/local/opt/xdelta[/format]

In unix its common to have variables in shell configuration file ex. bash_profile in your user home directory.

This example show 2 do shell script with different PATH

log (do shell script "echo $PATH")

set homeFolder to POSIX path of (path to home folder from user domain)
log (do shell script "source " & homeFolder & "/.bash_profile; echo $PATH")

If you have the brew install path as variable and then you could use unix command: source