Typing into Terminal works, AppleScript partially works
What can I do to make my AppleScript run things just like they run in Terminal?
Well, you could tell
the Terminal app to run your command:
tell application "Terminal" to do script "youtube-dl " & input
But, I don't recommend this route, because it will actually open a Terminal window!
There are a whole host of differences between do shell script
and typing commands into the Terminal. For starters, do shell script
commands are run via sh
, rather than the more advanced bash
or zsh
(And, your "#!/bin/zsh" shabang line at the top will have no effect in an Applescript!).
As to your actual problem, it sounds like youtube-dl can't find your copy of ffmpeg—probably because another difference between the Terminal app and do shell script
is your PATH is set differently. Try specifying the location of your ffmpeg binary with youtube-dl's --ffmpeg-location
flag.
If it helps to have a reference to look at, here is an Applescript I use to download videos from my web browser with youtube-dl. Note that I've been tweaking this for many years, so it has accumulated a lot of error checking, and code for compatibility with different browsers. Don't be intimidated! https://gist.github.com/Wowfunhappy/c2c8fc097a8b4f47430811fc1d0da041
osascript
#!/usr/bin/osascript
on open location input
do shell script "/usr/local/bin/youtube-dl " & quoted form of input
end open location
If run as a script, the hash-bang of your script should be the path to osascript
. This is the process that runs AppleScript (an Open Scripting Architecture language) on macOS.
Quote the URL passed into youtube-dl
to ensure it appears as a single argument to the tool.
Use the full path to youtube-dl
to ensure the tool is found. Use which youtube-dl
in Terminal to find the path.
Command Line Arguments
If the AppleScript is being passed the URL as the first command line argument, use the argv
handler:
#!/usr/bin/osascript
on run argv
do shell shell script "/usr/local/bin/youtube-dl " & quoted form of (item 1 of argv)
end run
If this is case, consider removing the script entirely and passing the argument directly to youtube-dl
.
Post Processing
youtube-dl
has the ability to post-process downloads. You can control this using a youtube-dl configuration file.
To specify the ffmpeg
binary to be used add the following line to either a file at /etc/youtube-dl.conf
or ~/.config/youtube-dl/config
:
--ffmpeg-location /usr/local/bin/ffmpeg
This looks an awful lot like a path problem - you have a script youtube-dl
and the program /usr/local/bin/youtube-dl
- in your AppleScript, try giving it the full path to your script.