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.
PATH
passes to the shell in ado shell script
command is:/usr/bin:/bin:/usr/sbin:/sbin
-- So if the executable is not within thatPATH
you need to use the fully qualified pathname or will need to preface the command(s) with anexport PATH=\"...\";
where...
represents thePATH
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.bash_profile
and.zprofile
. I guess I am confused because this wasn't an issue until I moved tozsh
. Do Apple Script scripts store paths in metadata when they are created? – John Nov 05 '21 at 19:41do 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:07do shell script
usessh
andsh
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:11bash
tozsh
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:34do shell script
command runs a non-interactive non-login shell using /bin/sh to interpret your command(s). If you rundo shell script "set"
you will see what you are working with. – user3439894 Nov 05 '21 at 21:02do shell script
command run from Script Editor, or from a Run AppleScript action in Automator. – user3439894 Nov 05 '21 at 21:17/etc/profile
and~/.profile
? You can also rundo 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