try
set theURL to getURLFromSafari()
--set theURL to getURLFromFirefox()
checkForYoutubeDL()
checkForFFmpeg()
do shell script "/usr/local/bin/youtube-dl --update"
set theName to ""
try
set theName to do shell script "/usr/local/bin/youtube-dl " & theURL & " --ignore-errors --no-check-certificate --no-playlist --get-title"
do shell script "/usr/local/bin/youtube-dl " & theURL & " -f bestvideo[vcodec^=avc1]+bestaudio[ext=m4a]/best --ignore-errors --no-check-certificate --no-playlist --ffmpeg-location /usr/local/bin/ -o '~/Movies/%(title)s.%(ext)s'"
display notification theName with title "Download Complete!" sound name "Hero"
on error errorMessage
if errorMessage contains "Unsupported URL" or errorMessage contains "Could not send HEAD request" then
display dialog "No downloadable videos found on web page." with icon stop buttons {"OK"}
error
else if errorMessage contains "geolocation" then
display dialog "This video contains geolocation restrictions." with icon stop buttons {"OK"}
error
else if errorMessage contains "too many requests" then
display dialog "Youtube has banned your IP address. Try again later." with icon stop buttons {"OK"}
error
else if errorMessage contains "SSL" then
display dialog "There was an SSL error. If you're running an old version of Mac OS, consider using an HTTPS proxy server like Squid to fix this problem." with icon stop buttons {"OK"}
error
end if
display dialog errorMessage with title "Unable to download video!"
error
end try
on error
display notification theName with title "Download Failed" sound name "Basso"
return
end try
on getURLFromFirefox()
try
tell application "Firefox"
activate
delay 0.2
tell application "System Events"
keystroke "l" using command down
delay 0.3
keystroke "c" using command down
delay 0.3
key code 53 --escape key
delay 0.3
keystroke "[" using command down
end tell
end tell
return (the clipboard) as text
on error
display dialog "Please open \"System Preferences\" → \"Security & Privacy\" → \"Privacy\" → \"Accessibility\" and add \"Firefox.app\" to the list of apps allowed to control your computer."
error
end try
end getURLFromFirefox
on getURLFromSafari()
try
tell application "Safari"
set theURL to URL of front document
set pageName to name of front document
--If it's possible to go back, then go back. Otherwise, close the tab instead.
if (do JavaScript "history.length" in front document) > 1 then
do JavaScript "history.back()" in front document
else
close current tab of front window
end if
end tell
on error
do shell script "defaults write com.apple.Safari IncludeDevelopMenu -bool true"
display dialog "In Safari, please enable Developer → Allow Javascript from Apple Events" buttons {"OK"}
error
end try
end getURLFromSafari()
on checkForYoutubeDL()
try
do shell script "/usr/local/bin/youtube-dl -h"
on error
display dialog "Please place a copy of youtube-dl in /usr/local/bin before using this script." with icon stop buttons {"OK"}
error
end try
end checkForYoutubeDL
on checkForFFmpeg()
try
do shell script "/usr/local/bin/ffmpeg -h"
on error
display dialog "Please place a copy of ffmpeg in /usr/local/bin before using this script." with icon stop buttons {"OK"}
error
end try
end checkForFFmpeg