Advertisement
MandyBrigwell

Untitled

Jan 3rd, 2025
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. try
  2. -- Set PATH to include Homebrew's default locations
  3. set shellPath to "PATH=/usr/local/bin:/opt/homebrew/bin:/usr/bin:/bin:/usr/sbin:/sbin"
  4.  
  5. -- Check if yt-dlp is installed via Homebrew
  6. do shell script shellPath & " command -v yt-dlp >/dev/null 2>&1"
  7. on error
  8. -- Prompt user to install yt-dlp via Homebrew
  9. display dialog "yt-dlp is not installed. Would you like to install it via Homebrew?" buttons {"Cancel", "Install"} default button "Install"
  10.  
  11. -- Install Homebrew if it's not found
  12. try
  13. do shell script shellPath & " command -v brew >/dev/null 2>&1"
  14. on error
  15. display dialog "Homebrew is not installed. Would you like to install it?" buttons {"Cancel", "Install"} default button "Install"
  16. do shell script "/bin/bash -c \"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\""
  17. end try
  18.  
  19. -- Install yt-dlp using Homebrew
  20. do shell script shellPath & " brew install yt-dlp"
  21. end try
  22.  
  23. tell application "Safari"
  24. -- Get the URL of the frontmost tab in Safari
  25. set theURL to URL of current tab of front window
  26. end tell
  27.  
  28. -- Remove playlist parameters from the URL
  29. if theURL contains "&" then
  30. set andOffset to offset of "&" in theURL
  31. set theURL to characters 1 thru (andOffset - 1) of theURL as string
  32. end if
  33.  
  34. -- Check if the URL has a valid video
  35. try
  36. -- Use yt-dlp to probe the URL (it will fail if no video is found)
  37. do shell script shellPath & " yt-dlp --get-title " & quoted form of theURL
  38. on error
  39. -- Display an error message if the URL is invalid or doesn't contain a video
  40. display dialog "The URL does not appear to contain a valid video." buttons {"OK"} default button "OK"
  41. return
  42. end try
  43.  
  44. -- Get video title and duration
  45. set videoInfo to (do shell script shellPath & " yt-dlp --get-title --get-duration " & quoted form of theURL)
  46. set videoTitle to paragraph 1 of videoInfo
  47. set videoDuration to paragraph 2 of videoInfo
  48.  
  49. -- Confirm download with the user
  50. display dialog "Do you want to download this video?
  51.  
  52. " & videoTitle & " (" & videoDuration & ")" buttons {"Cancel", "Download"} default button "Download"
  53.  
  54. -- Download the video to the Downloads folder
  55. do shell script "cd ~/Downloads && " & shellPath & " yt-dlp -f 'best' -i -o \"%(title)s.%(ext)s\" " & quoted form of theURL
  56.  
  57. -- Success message
  58. display dialog "Your video has been downloaded successfully." buttons {"OK"} default button "OK"
  59.  
  60. -- Update yt-dlp
  61. set updateResult to (do shell script shellPath & " brew upgrade yt-dlp || true")
  62. if updateResult contains "Upgrading" then
  63. display dialog "yt-dlp has been updated to the latest version." buttons {"OK"} default button "OK"
  64. end if
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement