Youtube-dl shell script M1 Mac

I just picked up a new M1 MBP for work and went to download some videos with youtube-dl and yt-dlp only to find that I can't get it to run in KM 10.0.1 via a shell script the way I've been able to before.

Results: /var/folders/n0/1v52npnn72gcbk_2tjs66z400000gp/T/Keyboard-Maestro-Script-441BD7BF-0DA5-4E6C-A2B6-CC7D727B5654: line 2: youtube-dl: command not found

I can run this command in the terminal no problem with bash. I'm still very new to the terminal so apologies if this isn't a KM issue.

youtube-dl.kmmacros (4.9 KB)

Would you be willing to validate what's in that bin directory by checking the results of this:

ls /usr/local/bin/y*

I have an M1 Mac Mini and there is nothing in that folder. I'm guessing that your previous Mac had that file loaded into that folder, and your new Mac hasn't yet been prepared the same way. But I could be wrong.

Assuming that you can run youtube-dl successfully in terminal .

You cannot reference $PATH without defining it manually in KM first.

So alternatively suggest using this line instead
source ~/.bash_profile

image

More info can be found at [action:Execute a Shell Script [Keyboard Maestro Wiki]]

1 Like

I suspect @Sleepy is on the right track here. If you installed youtube-dl through Homebrew, it will not be in /usr/local/bin. The Homebrew people changed the directory for executables on Apple Silicon Macs. It's now /opt/homebrew/bin. To confirm where youtube-dl is, run

which youtube-dl

in Terminal.

If you're a heavy user of Homebrew-installed utilities, you'll need to change several of your macros to reflect their new location. Or you can change Keyboard Maestro's ENV_PATH variable, as described in the article @macdevign_mac linked to.

2 Likes

I guess I wasn't a good UNIX admin because I forgot about "which." Actually I always remember what it does, I just keep forgetting that the command name is "which." It's kind of a dumb name.

Yes you can – give it a try...

It's not the same $PATH you see in the Terminal.app of course.

Many people don't have a ~/.bash_profile file, so one has to discover the correct customization file to use with source (if there is one). Unfortunately the Terminal doesn't help you do that – you have to know where to look.

I use ~/.profile myself.

@hayleyh's export PATH command is perfectly sufficient provided youtube-dl is installed in /usr/local/bin/

Since the command is not found it seems likely that u/l/b is the wrong parent directory for the command.

where is for only built-in stuff.
which for built-in OR 3rd party stuff ( think witch for magic :).

I use type myself most of the time.

-Chris

That's the issue, many a time, users who may not aware of that , trying on KM expect that PATH is the same as the one running on terminal. The reason is generally along this line "It run fine on terminal but on KM it didn't work.

By the way my youtube-dl reside in following path and this is added automatically when installing youtube-dl:

/Users/macdevign/opt/miniconda3/bin/youtube-dl

I suspect that the issue is more on PATH rather than on "/usr/local/bin" (there is a lot of "standard" paths added automatically by when we use stuff like homebrew, miniconda3).

But unfortunately unless hayleyh confirm the solutions given so far help to resolve the issue, there's no way to tell what is exactly the problem lies.

Thank you all for your thoughtful responses, this is making some sense to me now.

When I run which youtube-dl I get:

/opt/homebrew/bin/youtube-dl

Changing my shell script to this works:

export PATH=/opt/homebrew/bin:$PATH;

Changing it to these does not work for me:

source ~/.bash_profile
~/.profile

The goal for me would be to have these macros work with either path so I can use the same macros with Intel Macs as well. Sounds like I can do that by setting ENV_PATH differently on each computer, but I'm not understanding how I do that even after reading the wiki.

So you verify it in Terminal using: which youtube-dl ?

May I know if /opt/homebrew/bin is added in the $PATH variable in ~/.bash_profile ?

By the way, is $PATH in KM a empty string?

You set the path as shown with each path separated by a colon:

image

Although it's perfectly fine to have different ENV_PATHs on your different Macs, you don't have to. Including /opt/homebrew/bin in ENV_PATH on a computer that doesn't have that directory won't cause any problems. Same with /usr/local/bin.

Consider running echo $PATH on each of your machines and putting together a colon-separated string that includes all of the directories on both computers. You can set ENV_PATH to that string on both computers.

1 Like

Yeah I ran which YouTube-dl in terminal and got /opt/homebrew/bin

Being a total novice I have no idea how to answer your other questions... :grimacing: But I appreciate you trying. My way of getting around this atm will be to use a regular variable to set the path on each computer and just using that at the beginning of my shell script action.

Do as @drdrang mentions:

1- Open Keyboard Maestro > Preferences > Variables
2- Create a variable called ENV_PATH
3- Open Terminal, run echo $PATH
4- Copy-Paste the contents of echo $PATH in the ENV_PATH variable

Ok I'm back, many months later, because I want to understand what I'm doing wrong with this ENV_Path thing. I made the variable, I copied in the text from the terminal. What now? What do I write in the shell script action to call upon this variable? What replaces "export PATH=/usr/local/bin:$PATH;" in my original screenshot?

The name Needs to be All caps.

youtube-dl -F yoururlhere

If you created the ENV_PATH variable, you don’t need to add that line in your script. You can remove that line.

Thanks!!!

1 Like

See this:

Path in Shell Scripts

1 Like

Hi there, so I've been setting up a simple script to run at the beginning of my macros to ensure the ENV_Path variable is set correctly on whatever computer I set up the macros on, it looks like this:

Normally it does the trick, but on an M1 MacBook Pro I recently got this:
/usr/bin /bin /usr/sbin /sbin

Which didn't work, so I tried it in terminal and got this result which when I put it into my ENV_Path manually worked just fine:
/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin

How do I ensure that I'm getting an accurate result here? I'm trying to make this setup as simple as possible as I'm looking to share these macros.

What you are trying to echo here is the path that is set in the ENV_PATH variable. (As this is the path that is known to KM.)

That's the whole point of that variable. If KM used the path from the interactive shell, you wouldn't need to set ENV_PATH.

Unless this is entirely different on Apple silicon… (I am still on Intel Macs.)

Ok I see, so is there a way to retrieve the path from the interactive shell without manually copying and pasting it in?

Hello @hayleyh :wave:

Don’t do this :see_no_evil::see_no_evil::see_no_evil::see_no_evil::see_no_evil::see_no_evil:
Especially when you’re syncing your macros… if you want to use this approach- it will be better to turn off syncing.

If you want to switch between different $PATH Setups - your best chance is that you use the ~/.profile / ~/.bash_profile approach with the source command like @macdevign_mac & @ccstone talked about. Doing this you have the ability of manipulation in your $PATH on every machine like you need it for each particular use cases - and you don’t ever need to set ENV_PATH for Keyboard Maestro BUT you’ll have to use the absolutely same basics for the dotfiles setup and handling on each machine - regardless of the exact amount of $PATH configuration in detail.

If you just want the Variable - without using the dotfiles - @drdrang has you covered. He mentioned the right way to put two different ENV_PATH Values together in post #10. But this approach will need your time whenever you are changing the $PATH on either one or more Macs. Using this approach you can set the ENV_PATH Variable but here is one caveat you have to be aware of … the right order is very important.

Edit:
If you use both - Intel based Macs and Apple Silicon based ones - either approach will work when your macros are in sync as if there were only Intel based Macs or the Apple Silicon ones.

Greetings from Germany

Tobias