Why does my zsh program run fine in terminal, but not as an automator app? [duplicate]

I got a mac last week and am trying to learn the basics by coding a few projects I came up with. I've been creating a few apps I can place on the dock that automate simple functions that I use often. So far, all my programs have worked by creating a zsh script and getting Automator to 'Run Shell Script', however this particular program isn't fully working, although, if I run my program through terminal, it works perfectly.

I'm trying to create a program that can be executed after a YouTube url has been copied into the clipboard. The program takes the url and runs it through 'youtube-dl' and downloads the audio into the Downloads folder. I tried to learn how to do this from Z shell alone, but I was very overwhelmed with it and I decided to use python which I already know.

Here's what I'm doing:

My Python3 code:

import os

url = os.popen("echo `pbpaste`").read()
os.system("youtube-dl -f 'bestaudio[ext=m4a]' '"+url+"'")

My zsh file:

#!/bin/zsh

cd /Users/admin/Downloads
python3 /Users/admin/Documents/Scripts/Automation/ytGetAudio/ytGetAudio.py

I then made it executable with "chmod +x /Users/admin/Documents/Scripts/Automation/ytGetAudio/ytGetAudio.zsh"

When I run my program from the terminal, everything works great, and the file is downloaded into Downloads, but when I use Automator to "Run Shell Script" and I get it to run the same zsh script it doesn't download anything. I tested it by creating a pop-up message box in the python script; that worked, so the script is running, but something isn't working because nothing is downloading when I run the '.app' file.

Can anyone please help me with this?


Solution 1:

Thanks to the answers I got about paths, I was able to solve this. It lead me to this: My Automator Workflow fails because it fails to find the git command within the 'Run Shell Script' command? Need help which solved everything.

Added the following to the start of the script:

export PATH=/usr/local/bin:$PATH

Solution 2:

This is the standard issue with GUI apps in macOS. These are launched via lanuchd from desktop, no part of the process has run through a shell so what is in any of your shell start up scripts e.g. $PATH does not matter.

What you need to do is use the full path to anything you call in your case the full path to youtube-dl and if that is a script that might need editing to provide full paths to what it calls.