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.

John
  • 1,071
  • 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? – Marc Wilson Nov 05 '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" – user3439894 Nov 05 '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 05 '21 at 19:41
  • RE: "Do Apple Script scripts need to be "rehashed"?" -- What do you mean, what are you asking here? – user3439894 Nov 05 '21 at 20:02
  • @user3439894 the shell can store paths to recently used commands. I was pondering on that. Wondering if since I ran the script under bash and it worked it may have hashed the paths,,, but that makes no sense. The paths haven't changed. I added more to the post. – John Nov 05 '21 at 20:04
  • Each invocation of do shell script uses a new shell process, so state such as changes to variables and the working directory is not saved from one to the next. – user3439894 Nov 05 '21 at 20:07
  • @user3439894 I added more information in the original post. Something seems off. do shell script uses sh and sh knows where the executables are. Like I said, these scripts never required the full path to run before and some of the executables were/are not in the system default paths. – John Nov 05 '21 at 20:11
  • Not a single person can answer the question yet they are happy to mark it down. What has changed between moving from bash to zsh that would cause the same script to be unable to find an exec? Can't answer but willing to mark it down? – John Nov 05 '21 at 20:34
  • Changing your preferred interactive shell doesn't have anything to do with it. Your example script is no doubt finding pbpaste just fine but not finding gsed. Aside... is there some reason you need to use GNU sed? – Marc Wilson Nov 05 '21 at 20:39
  • 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. – user3439894 Nov 05 '21 at 21:02
  • @MarcWilson I answered your questions in the third comment – John Nov 05 '21 at 21:07
  • @MarcWilson, I am only talking about the do shell script command run from Script Editor, or from a Run AppleScript action in Automator. – user3439894 Nov 05 '21 at 21:17
  • What is in /etc/profile and ~/.profile? You can also run do shell script "env" to see what is set, SHELL should be pointing at zsh. You can set your path in ~/.profile and it should get picked up. – ian Jan 28 '22 at 06:53

1 Answers1

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.

Marc Wilson
  • 5,920
  • 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 05 '21 at 20:42
  • https://apple.stackexchange.com/questions/106355/setting-the-system-wide-path-environment-variable-in-mavericks/106814#106814 – John Nov 05 '21 at 20:47
  • 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. – Marc Wilson Nov 05 '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 06 '21 at 02:36