How do scripts run by Hazel get their environment?

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

For me, the most frustrating aspect of developing scripts that should be triggered by Hazel is this:

I develop and test the script in CodeRunner, where they work.

When I run them in Hazel and they fail, it is because the environment Hazel provides for the scripts is different/limited compared to the environment of my Terminal user:

- paths are missing or bogus (e.g. a path is /usr/gnu/bin; no idea where that comes from as it is set nowhere in /etc/paths, /private/etc/paths.d or .bash_profile. That also means no external command installed through a package manager is found when the script is run by Hazel.

- same with LOCALE; my LANG user settings are de_AT.UTF-8. Under Hazel, the scripts show a locale of en_AT.UTF-8, which is also bogus. Such a locale does not exist.

- settings for external commands are not found

Even after a lot of searching, I have not found an answer as to what environment information Hazel passes to the scripts it executes:

    Is this dependent on the environemnt of the parent/calling process?
    Is there a way to make sure that Hazel passes the same environment settings to the scripts it triggers?

Thanks,
Guy
WorkflowsGuy
 
Posts: 30
Joined: Thu Jul 04, 2013 3:55 pm

You need to set the environment yourself. The environment Hazel's scripts run in is relatively sparse. If you have a resource file that you use for your shell, you could source that file in your script.
Mr_Noodle
Site Admin
 
Posts: 11195
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

After a lot of research and experimentation, I found that using a shebang of
Code: Select all
#!/usr/bin/env -S bash --login
will make Bash shell scripts execute successfully from both the Terminal and in Hazel.

However, this does not work with scripts written e.g. in Python (at least I could not figure out a way how to do it). The recommended shebang of
Code: Select all
#!/usr/bin/env python3
will not work because Hazel does not pass on the environment of the current user and so the actual python3 binary is not found.

Hardcoding the actual path to the interpreter binary makes the script non-portable.

Another limitation is that python scripts which use modules that depend on external commands being available (eg. the textract module relies on the presence of the pdftotext binary somewhere in the PATH) will not work in Hazel.

So my big feature request is that Hazel behave like a Terminal program, reading the environment of the current user and passing it on the user scripts it executes.

Thanks,

Guy
WorkflowsGuy
 
Posts: 30
Joined: Thu Jul 04, 2013 3:55 pm

There was a point where it did but that introduced way more bugs with scripts not running as expected for various reasons, hence the state things are in now.
Mr_Noodle
Site Admin
 
Posts: 11195
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Support