Automate the download of YouTube videos in Safari 5.1

Mar 08, '12 07:30:00AM

Contributed by: philostein

A YouTube video can be downloaded by Option-double-clicking its video URL in Safari's Activity window. (See this hint.) However, if a lot of tabs are open, the URL can be difficult to find; particularly as now it will often have no "MB" value in the Status column.

This script will find the YouTube video URL of the current tab in the Activity window, and attempt to download it in Safari. You will need to always have at least one download in the Downloads popover to make it available! Otherwise, the script leaves the video URL highlighted for an Option-double-click download. Run the script with your favourite app launcher.

It works by first searching the Activity window for the name of the current Safari tab, then opening the disclosure triangle to find the "videoplayback" URL. Then, if its available, the "Show Downloads" button is clicked and its popover is brought into focus. Finally, the script pastes in the video URL for downloading.

There are some delays in the script that can be un-commented if you find it goes too fast for the windows on your Mac to keep up with. (The delays are not necessary on my mid-2011 MacBook Air.) Change the _videoName value if there's another kind of unique element you'd like to download instead.

A couple of incidental things I noticed about the Downloads popover: it can only be brought into focus by clicking its list first (clicking its buttons won't work otherwise), and clicking "Clear" when a file is downloading will clean up its list without removing the downloads button from Safari's toolbar.

Here's the AppleScript code:
try
	set _videoName to "videoplayback"
	set _downloadButtonAvailable to true
	
	tell application "Safari" to activate
	-- delay 0.5
	tell application "Safari" to set _windowName to name of current tab of window 1
	
	
	tell application "System Events"
		tell process "Safari"
			try
				set _downloadButton to item 1 of (every button whose description = "Downloads") of tool bar 1 of window _windowName
			on error a number b
				set _downloadButtonAvailable to false
			end try
			
			tell menu item "Activity" of menu "Window" of menu bar item "Window" of menu bar 1 to click
			try
				window "Activity"
			on error a number b
				tell menu item "Activity" of menu "Window" of menu bar item "Window" of menu bar 1 to click
			end try
			
			-- delay 0.5
			
			tell outline 1 of scroll area 1 of window "Activity"
				
				set _count to count of rows
				try
					repeat with i from 1 to _count
						tell group 1 of row i
							try
								try
									set _triangleValue to value of UI element 1
									if _triangleValue = 1 then
										tell UI element 1 to click
										-- delay 0.2
									end if
									tell text field 1 to set _value to value
									if _value = _windowName then exit repeat
								end try
								value of text field 1
							on error a number b
								exit repeat
							end try
						end tell
					end repeat
				end try
				
				set _count to count of rows
				
				repeat with n from 1 to _count
					try
						tell group 1 of row n
							tell text field 1 to set _value to value
							
							if _value = _windowName then
								set _row to n
								set _triangleValue to value of UI element 1
								if _triangleValue = 0 then tell UI element 1 to click
								exit repeat
							end if
						end tell
					end try
				end repeat
				
				set _count to count of rows
				
				repeat with x from _row to _count
					try
						tell row x
							tell text field 1 to set _value to value
							if _value contains _videoName then
								set _url to value of text field 1
								if _downloadButtonAvailable = false then select
								exit repeat
							end if
						end tell
					end try
				end repeat
				
			end tell
			
			try
				_url
			on error a number b
				error "The '" & _videoName & "' URL couldn't be found."
			end try
			
			if _downloadButtonAvailable = true then tell menu item "Activity" of menu "Window" of menu bar item "Window" of menu bar 1 to click
		end tell
	end tell
	
	
	if _downloadButtonAvailable = false then
		tell application "Safari"
			activate
			display dialog "The 'Show Downloads' button is not available. 
			
Option-click the selected '" & _videoName & "' URL to download the video." with title "Safari YouTube Video Downloader"
		end tell
		
	else
		set the clipboard to _url
		-- delay 0.5
		
		tell application "System Events"
			tell process "Safari"
				try
					pop over 1 of _downloadButton
				on error a number b
					click _downloadButton
				end try
				set focused of list 1 of scroll area 1 of pop over 1 of _downloadButton to true
			end tell
			-- delay 0.5
			keystroke "v" using command down
		end tell
	end if
	
on error a number b
	if a does not contain "user canceled." then tell application "Safari" to display dialog a with title "Safari YouTube Video Downloader"
end try
[kirkmc adds: I tried this with a few videos, and it works. That's some pretty impressive AppleScripting. One question: can't you make the Downloads popover visible by sending a Command-Option-L keystroke?]

Comments (11)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20120308034410310