[SOLVED] Shell script not working with KM, but works with Terminal

Let me pause to think. When you said your script didn't work, do you mean you ran it from your KM Execute Shell script action?

We know with 99% certainity that the reason it works from the Terminal is that your PATH is set correctly in your current Terminal, but not in new Terminals.

Yes, I'm not using Terminal. Just KM

And do you get the same error that says "Could not find ffmpeg in your $PATH" in the body of the message?

What do you get now when you run the KM action I pasted above, containing "env | grep PATH". Probably something different from before, right?

As a side note... (don't do this) ... if you closed your Terminal app completely and reopened it, the program won't run, because you have the PATH and other env variables set up only in that one Terminal window. This is why it works for you in one window but not in KM. You could prove this by opening another Terminal window and trying it from there. It probably won't work, I think. (Hard to be certain.)

Ok I'm gonna be honest, I'm ver confused now...
What do you mean by:

Am I supposed to add that to the code somewhere? Or was that supposed to just test something?

Let me clarify so we can be on the same page:
This is that file you asked me to modify
image

This is my macro (only has 1 action)
image

And I get this

2023-11-03 23:10:07 Action 15299675 failed: Execute a Shell Script failed with script error: Traceback (most recent call last):
File "/opt/anaconda3/bin/ffmpeg-normalize", line 8, in
sys.exit(main())
File "/opt/anaconda3/lib/python3.9/site-packages/ffmpeg_normalize/main.py", line 496, in main
ffmpeg_normalize = FFmpegNormalize(
File "/opt/anaconda3/lib/python3.9/site-packages/ffmpeg_normalize/_ffmpeg_normalize.py", line 116, in init
self.ffmpeg_exe = get_ffmpeg_exe()
File "/opt/anaconda3/lib/python3.9/site-packages/ffmpeg_normalize/_cmd_utils.py", line 171, in get_ffmpeg_exe
raise FFmpegNormalizeError(
ffmpeg_normalize._errors.FFmpegNormalizeError: Could not find ffmpeg in your $PATH or $FFMPEG_PATH. Please install ffmpeg from http://ffmpeg.org
2023-11-03 23:10:07 Execute a Shell Script failed with script error: Traceback (most recent call last):
File "/opt/anaconda3/bin/ffmpeg-normalize", line 8, in
sys.exit(main())
File "/opt/anaconda3/lib/python3.9/site-packages/ffmpeg_normalize/main.py", line 496, in main
ffmpeg_normalize = FFmpegNormalize(
File "/opt/anaconda3/lib/python3.9/site-packages/ffmpeg_normalize/_ffmpeg_normalize.py", line 116, in init
self.ffmpeg_exe = get_ffmpeg_exe()
File "/opt/anaconda3/lib/python3.9/site-packages/ffmpeg_normalize/_cmd_utils.py", line 171, in get_ffmpeg_exe
raise FFmpegNormalizeError(
ffmpeg_normalize._errors.FFmpegNormalizeError: Could not find ffmpeg in your $PATH or $FFMPEG_PATH. Please install ffmpeg from http://ffmpeg.org. Macro “Normalize MP3 -1dB (available via Raycast)” cancelled (while executing Execute Shell Script).

Yes that is the file I asked you to modify.

And yes that is the correct contents.

And the KM script you are showing me is probably fine. I'm not paying much attention to KM in this thread because your problem has nothing to do with KM. KM is entirely unrelated to your problem. I'm 99% sure about that.

Your problem is that your path is not correct. The conda script failed to set up your path for any new shells that are opened, including the KM shell. We can (probably) prove this if you open a new Terminal window and check your path in that.

Oh, and you asked one more thing. What I wanted you to do was run this again, now that you've changed .zshrc we should get a different result.

I was also asking you to open a new Terminal window and run "env | grep PATH" from there.

I apologize if this is confusing. It is probably overwhelming. This comes from my years of experience fixing stupid broken UNIX programs.

Ok so let me start from scratch...
I closed Terminal and opened it again.
Ran env | grep PATH on both Terminal and KM, BEFORE the .zshrc file was modified
Results:

Terminal
SQLITE_EXEMPT_PATH_FROM_VNODE_GUARDS=/Users/dannywyatt/Library/WebKit/Databases
PATH=/opt/anaconda3/bin:/opt/anaconda3/condabin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin

KM
PATH=/usr/bin:/bin:/usr/sbin:/sbin

Then changed the file to this to only contain this
export PATH="/opt/anaconda3/bin:$PATH"

Ran env | grep PATH on both Terminal and KM. Results:
Terminal

SQLITE_EXEMPT_PATH_FROM_VNODE_GUARDS=/Users/dannywyatt/Library/WebKit/Databases
PATH=/opt/anaconda3/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin

KM
PATH=/usr/bin:/bin:/usr/sbin:/sbin

So in KM the results are the same. Terminal changed to something else

No problem. It is overwhelming indeed, just because I have no idea what I'm doing and not being able to provide any meaningful feedback and options, makes it even worse...
It's not about you, it's about me not having any clue what's happening and not being able to participate

Your KM result does not include the path we put into your .zshrc file? My mind is blown. KM is supposed to get its data from there, I thought. Let me double check that.

There's probably another way to fix this, but less perfect. We just add the path inside your KM execute shell action. But that's not clean. Give me another minute to think.

I think I will revert the file to what it was and I will get in touch with the ffmpeg team, see if they have a solution for this.
If not, I will have to rely on an app to do the same thing. Not a huge problem, I just wanted to create a macro to make it easier and faster.

Sorry for making you spend so much time on this. Appreciate your help on this. :+1:

As you wish. Don't feel bad for making me think. There are other ways to fix this, but ultimately the company that makes the script should make it work for macOS.

The KM action and macro will probably work as soon as the PATH is incorporated into your environment. We were very close, but even then we weren't actually fixing the problem which was their faulty script.

I have another script and it works. This one we were looking at is "ffmpeg-normalize". The other one is just "ffmpeg".
I would expect that the path was related to those files, but as you mentioned, there's more to it and I'm afraid I can't help much with my contribution...

For example the other one that's working (just ffmpeg, instead of ffmpeg-normalize):

/opt/anaconda3/bin/ffmpeg -i "/Users/dannywyatt/My Files/Music Projects/Danny Wyatt/Music/_Assets Promo Videos/background 59 seconds.mp4"
-i "/Users/dannywyatt/My Files/Music Projects/Danny Wyatt/Music/_Assets Promo Videos/Song $KMVAR_Local__SongNumber/Facebook-Feed.png"
-filter_complex "[0:v][1:v] overlay=0:0"

Not all programs have the same environment requirements. Results can differ for different programs.

Even though you appear to have given up with my train of thought, this might also fix your problem...

image

...but I didn't want you to have to insert that line each time you wanted to run normalize. But there's a good chance if you test this, it will confirm what I've been saying.

1 Like

Have you installed ffmpeg-normalize? It's not included in ffmpeg. Sorry if you've already covered this; it's a long thread and it's been a long day!

Yes, I installed it today I can normalize the files via Terminal

@noisneil, do you know why KM's Execute Shell script action doesn't run .zshrc?

Yes, adding that extra line to the action made it work
export PATH="/opt/anaconda3/bin:$PATH"
/opt/anaconda3/bin/ffmpeg-normalize "/Users/dannywyatt/My Files/Inbox Global/Danny Wyatt - Cookies.mp3" -f -c:a libmp3lame -b:a 320k -nt peak -t -1 --output "/Users/dannywyatt/My Files/Inbox Global/Danny Wyatt - Cookies normalized.mp3"

Is it bad that I have it like that?
I mean, it's working now...
That doesn't mess with any other files or anything, right?

Hooray! I was right all along. My solution is fine. It won't hurt a thing. I was just trying desperately to give you a "more right" and easier solution.

But ultimately this is a flaw in your product and/or its init program which is supposed to set up your .zshrc file for macOS, and it isn't working.

There's a chance that something else will also be broken. We fixed it for one program, but others could also be broken for similar or different reasons. But this fix is a standard fix that I've had to do a dozen times in my past.

1 Like

I don't but ChatGPT thinks it does:

"Interactive configuration files like .zshrc are only read by interactive shells, and .bash_profile, .profile, or .zlogin are read by login shells. When a shell script is executed from within an automation tool, it's usually run in a non-interactive, non-login shell, which doesn't read these files by default.

If you need to execute something that is defined in your .zshrc file, you can source the file at the beginning of your script manually. Here's an example of how you might do that:

source ~/.zshrc
# ... rest of your script ...

However, be cautious when doing this, as .zshrc can contain interactive elements that don't make sense in a script context and might cause errors or unexpected behavior.

It's usually better to explicitly define any environment variables or paths directly in your script or use a separate configuration file that contains only the necessary components for your script to run."

Wow. Chat GPT is talented in this topic, and probably right. I forgot those details. I also don't use ChatGPT much to solve my problems. I know it's the future.

As long as it works and it doesn't mess with other files, I'm totally ok with having an extra line :slight_smile:

Ok now back to the initial problem (remember the double quotes, etc, @noisneil?)

Here's the original script with the FOR EACH action using the variable Local__mp3:
export PATH="/opt/anaconda3/bin:$PATH"
/opt/anaconda3/bin/ffmpeg-normalize "$KMVAR_Local__mp3" -f -c:a libmp3lame -b:a 320k -nt peak -t -1 --output "$KMVAR_Local__mp3 - normalized"

The reason it wasn't working is because when we use "$KMVAR_Local__mp3", it's using the full path including the extension, in this case .mp3
So when I use "$KMVAR_Local__mp3 - normalized" the final result is something like this:
"Danny Wyatt - Cookies.mp3 - normalized"

The solution for this is to first remove the extension from the variable and then adding it manually in the script

image

I tested it and it's working.
I noticed it by accident in the engine logs:

1 Like