Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- try
- -- Set PATH to include Homebrew's default locations
- set shellPath to "PATH=/usr/local/bin:/opt/homebrew/bin:/usr/bin:/bin:/usr/sbin:/sbin"
- -- Check if yt-dlp is installed via Homebrew
- do shell script shellPath & " command -v yt-dlp >/dev/null 2>&1"
- on error
- -- Prompt user to install yt-dlp via Homebrew
- display dialog "yt-dlp is not installed. Would you like to install it via Homebrew?" buttons {"Cancel", "Install"} default button "Install"
- -- Install Homebrew if it's not found
- try
- do shell script shellPath & " command -v brew >/dev/null 2>&1"
- on error
- display dialog "Homebrew is not installed. Would you like to install it?" buttons {"Cancel", "Install"} default button "Install"
- do shell script "/bin/bash -c \"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\""
- end try
- -- Install yt-dlp using Homebrew
- do shell script shellPath & " brew install yt-dlp"
- end try
- tell application "Safari"
- -- Get the URL of the frontmost tab in Safari
- set theURL to URL of current tab of front window
- end tell
- -- Remove playlist parameters from the URL
- if theURL contains "&" then
- set andOffset to offset of "&" in theURL
- set theURL to characters 1 thru (andOffset - 1) of theURL as string
- end if
- -- Check if the URL has a valid video
- try
- -- Use yt-dlp to probe the URL (it will fail if no video is found)
- do shell script shellPath & " yt-dlp --get-title " & quoted form of theURL
- on error
- -- Display an error message if the URL is invalid or doesn't contain a video
- display dialog "The URL does not appear to contain a valid video." buttons {"OK"} default button "OK"
- return
- end try
- -- Get video title and duration
- set videoInfo to (do shell script shellPath & " yt-dlp --get-title --get-duration " & quoted form of theURL)
- set videoTitle to paragraph 1 of videoInfo
- set videoDuration to paragraph 2 of videoInfo
- -- Confirm download with the user
- display dialog "Do you want to download this video?
- " & videoTitle & " (" & videoDuration & ")" buttons {"Cancel", "Download"} default button "Download"
- -- Download the video to the Downloads folder
- do shell script "cd ~/Downloads && " & shellPath & " yt-dlp -f 'best' -i -o \"%(title)s.%(ext)s\" " & quoted form of theURL
- -- Success message
- display dialog "Your video has been downloaded successfully." buttons {"OK"} default button "OK"
- -- Update yt-dlp
- set updateResult to (do shell script shellPath & " brew upgrade yt-dlp || true")
- if updateResult contains "Upgrading" then
- display dialog "yt-dlp has been updated to the latest version." buttons {"OK"} default button "OK"
- end if
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement