2

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

1

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
  • This is exactly what I was looking for! Thank you! I know I can customize the notification from here using the documentation. Can you explain the pid/track progress? I'm new to applescript, so I'm not sure how I could implement that. – William Dec 11 '15 at 15:39
  • You're welcome! Keep in mind this is not a very solid/fast solution :) Tried to read the received network data for the download process and, somehow, feed that as a continuous update to applescript dialog/notification.. Not sure how to do either one ;) youtube-dl has a python class that could be convenient to use. – EO2 Dec 14 '15 at 5:49
0

You have to check the return value of the shell script. See Apple's Documentation on do shell in the section Getting an Answer.

How does do shell script report errors?

All shell commands return an integer status when they finish: zero means success; anything else means failure. If the script exits with a non-zero status, do shell script throws an AppleScript error with the status as the error number.

set success to do shell script "cd /tmp/; /usr/local/bin/youtube-dl " & theUrl

So you can then check the success variable.

if success is equal to 0

You can probably also use try and on error to check if the download worked.

try
    do shell script "cd /tmp/; /usr/local/bin/youtube-dl " & theUrl
on error
    -- show error message
end try

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .