Understanding the Scripting Environment
If you have a script that works in the Terminal but not Alfred, chances are it's due to a different environment: a Unix concept whose modern form has been a part of computing since the late 70's. You can read about in on Wikipedia but the gist is they are values that affect how processes run on your machine.
When you open a Terminal, it launches a shell (the Z shell by default on macOS) which loads a set of configuration files containing default values and - more importantly - your custom settings. The latter are bound to be the cause of the different behaviour you're seeing between Alfred and the Terminal.
The most likely culprit will be PATH
, which defines where tools check for executables. It's what allows you to run ls
instead of /bin/ls
. It starts out short and expands as you set up your system. For example, you may have changed it to include a personal scripts directory at ~/my-scripts
.
By design, Alfred does not see this custom environment. Its PATH
is /opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
and isn't affected by your shell's configuration files.
Note: This includes the default install locations for Homebrew on both Intel and Apple Silicon Macs. Prior to Alfred 5, Alfred’s
PATH
was/usr/bin:/bin:/usr/sbin:/sbin
.
Making your Workflow mostly independent of external settings benefits you both as a Workflow developer and user: by ensuring everyone has a close starting point, changing this environment must be a conscious decision with clear consequences. When you do want to make that choice, the best method is the one which modifies the least:
- Method 1: Use the full path to your tool. Instead of
do-something
, call~/my-scripts/do-something
. Simplest method. - Method 2: Set
PATH
before calling your tools. This can be done before calling your script or in the Workflow Environment Variables. - Method 3:
source
your shell's configuration file before calling your script. Example:source "${HOME}/.zshrc"
. May make your script slower due to loading unnecessary configurations, but may be necessary if your needs are specific.
If the first method is viable, consider copying your tool into the Workflow's directory. You can access it from the Workflow by using relative paths.