Wrapping youtube-dl into automator via applescript

applescriptautomatorterminal

I am hoping to make an automator application that prompts the user for input (i.e. youtube url), and downloads it using youtube-dl (already installed via Homebrew). After this, I am trying to use the command youtube-dl URL_HERE -e which fetches the title of the video. I am hoping to make a notification that says "successfully downloaded TITLE_VIDEO" after it downloads successfully (if it downloads succesfully). I am using the "Run Applescript" command of automator.

This is all I've gotten so far:

display dialog "What is the youtube URL you want to download?" default answer ""
set answer to text returned of result

tell application "Terminal"
    activate
    do script with command "youtube-dl -f 140 " & answer
end tell

display notification "Successfully downloaded " & TITLE_HERE sound name "Blow"

I'm new to applescript, so only basic functions/ones that can be explained easily.

What I'm thinking is to do the terminal command youtube-dl URL_HERE -e which will return the title of the video (TITLE), and set that to a variable and then pass that variable to display notification

Best Answer

Nice - a simple UI like that could be very useful in many situations!

Try this:

display dialog "URL to fetch?" default answer ""
set theUrl to (text returned of result)

--> get file
do shell script "cd /tmp/; /usr/local/bin/youtube-dl " & theUrl

--> get title
set theTitle to do shell script "/usr/local/bin/youtube-dl -e " & theUrl

display notification "Fetched " & theTitle sound name "Blow"
delay 1 --> avoid quit before notice

Unfortunately the youtube-dl script did not actually download the video when using the -e flag, (and an error occurred when trying to use the --exec CMD option).

Ended up just running the command twice; First download to /tmp, then get title.

Could also use the process id (pid) to track progress, etc.. :)

command 2>/dev/null & pid=$!

Update: I could not get the output of certain commands through applescript, like 'nettop'. There are probably ways to fix it. Here is my sloppy attempt at implementing a pid checking.. :)

set question to display dialog "URL to fetch?" default answer "" buttons {"Cancel", "Open in Browser", "Download"} default button 3
set pageURL to (text returned of result)
if pageURL is "" then return "No URL"
set choice to (button returned of question)

if choice is "Download" then
    try
        set pid to do shell script "cd /tmp/; /usr/local/bin/youtube-dl --newline " & pageURL & " > /tmp/vidstatus 2>&1 & echo $!"
        delay 1
        repeat while ((do shell script "kill -0 " & pid) is "") -- check if pid is still responding
            display dialog "Status: " & (do shell script "tail -n1 /tmp/vidstatus") -- display last line of output
        end repeat
    on error
        display dialog "Download complete!" -- eh, success, hopefully :) 
    end try

else if choice is "Open in Browser" then
    try
        set videoURL to do shell script "/usr/local/bin/youtube-dl -g " & pageURL
        open location videoURL -- browser can stream and save file
    on error
        display dialog "Download aborted!"
    end try
end if