0

Since moving to zsh from bash my previous AppleScript scripts can no longer find executables that are in my path, unless I use the full path to the executables.

What can I do to keep using zsh and stop having to enter the full path to the executable in my AppleScript scripts?

I feel like I am missing some configuration setup for zsh or for AppleScript.

11/05/21_14:20:53 /Users/john
$ mypath
/Users/john/.pyenv/shims
/usr/local/sbin
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
/opt/X11/bin
/Library/Apple/usr/bin

example script was not working:

do shell script "pbpaste | gsed 's/\\x2D/\\xE2\\x96\\xB8/g' | LANG=en_US.UTF-8 pbcopy"

working with full paths to execs:

do shell script "/usr/bin/pbpaste | /usr/local/bin/gsed 's/\\x2D/\\xE2\\x96\\xB8/g' | LANG=en_US.UTF-8 pbcopy"

Would it have anything to do with running these scripts under bash first. sh does know where the executables are.

sh-3.2$ which gsed
/usr/local/bin/gsed
sh-3.2$ which pbpaste
/usr/bin/pbpaste
sh-3.2$ 

EDIT_1: From Apple Technical Note TN2065

Second, when you use just a command name instead of a complete path, the shell uses a list of directories (known as your PATH) to try and find the complete path to the command. For security and portability reasons, do shell script ignores the configuration files that an interactive shell would read, so you don’t get the customizations you would have in Terminal. Use the full path to the command, for example, /sbin/ifconfig instead of just ifconfig. To find the full path in Terminal, say which command-name, for example, which ifconfig; to see the list of places do shell script will search, say do shell script "echo $PATH".

Still don't understand why all of these scripts worked without the full path when bash was my default shell and are all broken now that zsh is my default.

EDIT_2 THIS!

Where does the shell environment come from — environment variables, working directory, and so on? do shell script inherits the environment of its parent process, which is always the process running the script. The environment covers the working directory, any environment variables, and several other attributes — see execve(2) for a complete list. As mentioned in Issuing Commands, do shell script does not read the configuration files that an interactive shell running in Terminal would.

Any application launched from the Finder gets the same default environment: a working directory of / and the environment variables HOME, LANG, PATH, SHELL, and USER. Most applications do not change their environment, but relying on this is a maintenance risk.

12
  • Because however you are invoking these scripts does not set the path you think it does. What IS the path while one of these is running? Where do you set your path and how are you invoking these scripts? Nov 5 '21 at 19:36
  • The default PATH passes to the shell in a do shell script command is: /usr/bin:/bin:/usr/sbin:/sbin -- So if the executable is not within that PATH you need to use the fully qualified pathname or will need to preface the command(s) with an export PATH=\"...\"; where ... represents the PATH you want to use . E.g.: do shell script "export PATH=\"...\"; pbpaste | gsed 's/\\x2D/\\xE2\\x96\\xB8/g' | LANG=en_US.UTF-8 pbcopy" Nov 5 '21 at 19:37
  • I see. Looking at Apple Technical Note TN2065 has some info. I am invoking it using Script Editor in the Finder Menu bar. My paths are set in both .bash_profile and .zprofile. I guess I am confused because this wasn't an issue until I moved to zsh. Do Apple Script scripts store paths in metadata when they are created?
    – John
    Nov 5 '21 at 19:41
  • RE: "Do Apple Script scripts need to be "rehashed"?" -- What do you mean, what are you asking here? Nov 5 '21 at 20:02
  • 1
    The do shell script command runs a non-interactive non-login shell using /bin/sh to interpret your command(s). If you run do shell script "set" you will see what you are working with. Nov 5 '21 at 21:02
1

Unless you've done something highly creative, Script Editor is using sh to run shell scripts. It does not use your configured shell.

That points to a copy of bash by default.

When bash is invoked (as a login shell) as sh, it only reads /etc/profile and ~/.profile. But it reads no startup files otherwise. So it is not reading any shell configuration you think you are providing.

So you should be just inheriting from the system. enter image description here Note that pbpaste is findable since it's in /usr/bin and the default path includes /usr/bin.

Since that doesn't work, something else has been changed.

4
  • What shell does do shell script use, really? do shell script always calls /bin/sh. However, in macOS, /bin/sh is really bash emulating sh.
    – John
    Nov 5 '21 at 20:42
  • 1
    Which is irrelevant, when it's invoked as sh it is reading no startup files at all, it is simply inheriting the system environment. Nov 5 '21 at 20:50
  • Well... They clearly worked when I created them and used them for a year before I moved to zsh. The clearly don't work now. That's the conundrum and why I posted. I will keep working on what changed to cause this.
    – John
    Nov 6 '21 at 2:36

You must log in to answer this question.

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