Page 1 of 1

$PATH in Hazel scripts

PostPosted: Thu Feb 06, 2025 2:29 am
by halloleo
Hey Mr Noodle

I have quite a few rules with embedded scripts which read a file and pass back a new file name based on content analysis. I do this analysis with a xonsh (https://xon.sh/) script.

The embedded Applescript looks like this:

Code: Select all
set filePath to POSIX path of theFile
set shellCmd to "/opt/homebrew/bin/xonsh --no-rc $HOME/opt/link/bin/name-for-CSC-statement.xsh" & " " & quoted form of filePath

set shellOut to do shell script shellCmd if shellOut is not equal to "" then
   set theRes to {hazelPassesScript:true, hazelOutputAttributes:{shellOut}} else
   set theRes to {hazelPassesScript:false} end if


This feels to me pretty convoluted and error prone because Hazel does not use my $PATH variable. (Both /opt/homebrew/bin/ and $HOME/opt/link/bin are in my $PATH.)

Is there a way to let Hazel search for executables in my $PATH variable?

Many thanks,
Leo

Re: $PATH in Hazel scripts

PostPosted: Thu Feb 06, 2025 10:02 am
by Mr_Noodle
You will need to set it yourself. If it is set in one of your shells startup files, you can try sourcing/executing that though you have to be careful as some of those files may contain things only meant for interactive shells.

Re: $PATH in Hazel scripts

PostPosted: Mon Feb 10, 2025 11:48 pm
by halloleo
Thanks for your reply.

Mr_Noodle wrote:You will need to set it yourself. If it is set in one of your shells startup files, you can try sourcing/executing that though you have to be careful as some of those files may contain things only meant for interactive shells.


With a shell startup file I could share the directory of the script ($HOME/opt/link/bin/), right? But how can I share the directory the shell it self is in (/opt/homebrew/bin/)?

Re: $PATH in Hazel scripts

PostPosted: Tue Feb 11, 2025 9:28 am
by Mr_Noodle
Not sure if I follow. If you export your PATH var, you can stick whatever you want in it.

Re: $PATH in Hazel scripts

PostPosted: Tue Feb 11, 2025 9:34 pm
by halloleo
Mr_Noodle wrote:Not sure if I follow. If you export your PATH var, you can stick whatever you want in it.


Sorry, Mr Noodle. I rephrase it. I also understand the situation better now:

The problem is that in the Hazel action Run applescript the command
Code: Select all
do shell script shellCmd
doesn't use the $PATH I have set for the shell. That's why in the action xonsh without a path is not found and I have to write /opt/homebrew/bin/xonsh.

Howver a shell command run through the Hazel action Run shell script does use my $PATH, so in this action writing plainly "xonsh" works.

Therefore, my ultimate question is: Can I do all the HazelOutputAttribute magic I have done in the Applescript from a shell script instead?

I hope this is clearer now.

Re: $PATH in Hazel scripts

PostPosted: Wed Feb 12, 2025 9:37 am
by Mr_Noodle
There is no way currently to export attributes using a shell script. You will need to use Apple/JavaScript for that. Note when using a shell script, you are specifying the path somewhere. If not in the script, then in the shell field itself.

Re: $PATH in Hazel scripts

PostPosted: Wed Feb 12, 2025 11:26 pm
by halloleo
Thanks, Mr. Noodle. Good to know.