Alfred 5.5 is here - Find out what's new

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 an interactive 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, as shells don't load these files when run from independent contexts such as Alfred.

Note: ~/.zshenv is a notable exception. It is loaded in every environment by default, which can affect workflow behaviour. Alfred defaults to /bin/zsh --no-rcs to avoid this.

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.

Alfred's defined PATH is /opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin, which includes the default install locations for Homebrew on both Intel and Apple Silicon Macs.

Note: Prior to Alfred 5, Alfred's PATH was /usr/bin:/bin:/usr/sbin:/sbin.

Making your Workflow independent of external settings benefits you both as a Workflow developer and user: by ensuring a common 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. If viable, consider copying it into the Workflow's directory and using relative paths.
  • 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.

Latest Blog Post:

Discover Alfred Workflows: Languages and References

CacheFly Campaign Monitor

"Alfred" is a registered trademark of Running with Crayons Ltd. ©2024 Running with Crayons Ltd. All rights reserved.

Terms & Conditions, Privacy Policy, Cookies.