Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Automate the download of YouTube videos in Safari 5.1 Web Browsers

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?]
    •    
  • Currently 3.00 / 5
  You rated: 2 / 5 (7 votes cast)
 
[15,611 views]  

Automate the download of YouTube videos in Safari 5.1 | 11 comments | Create New Account
Click here to return to the 'Automate the download of YouTube videos in Safari 5.1' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Automate the download of YouTube videos in Safari 5.1
Authored by: morespace54 on Mar 08, '12 08:44:11AM

Great script!
Now, wouldn't be even better to go by extension (.flv) instead of filename (videoplayback)?
Maybe it's harder as you might end up with the "wrong" file being downloaded...



[ Reply to This | # ]
Automate the download of YouTube videos in Safari 5.1
Authored by: boredzo on Mar 08, '12 11:20:04AM

The videoplayback URL doesn't have a filename extension. After the word “videoplayback”, which is the filename, comes a query string (“?” followed by a bunch of name-value parameters).

Also, the desired video file may not be a .flv file. Depending on the selected quality setting in the player, it may be a .mp4 file instead.



[ Reply to This | # ]
Automate the download of YouTube videos in Safari 5.1
Authored by: wallybear on Mar 08, '12 10:32:54AM

Great work, but it could be simpler to use one of the many Safari extensions for this task.
In particular I use YouTube5 (http://www.verticalforest.com/youtube5-extension/), that privileges html5 video format on YouTube and similar sites (but you can choose Flash contents also) and enable you to download the video choosing from formats and resolutions available. It works also on facebook.



[ Reply to This | # ]
Automate the download of YouTube videos in Safari 5.1
Authored by: philostein on Mar 08, '12 02:21:10PM
@kirkmc Thanks! And an impressive turnaround, too. (I submitted this hint less than 12 hours ago.)

Using System Events, it's possible to do this:
tell menu item "Show Downloads" of menu "View" of menu bar item "Window" of menu bar 1 to click
But the script needs the downloads button to see if pop over 1 is visible, so I figured I'd just get that.

[ Reply to This | # ]
Automate the download of YouTube videos in Safari 5.1
Authored by: Bairnsfather on Mar 08, '12 05:18:35PM

Admittedly not automating video downloads, but at least making downloading less work, and putting links up for each available resolution, is http://iCab.de.

I used to hunt around the Activity window too. Now I just open the YouTube page in iCab and at the top of the window is a list of the video in every resolution available.

The only caveat is the filter to accomplish this is not installed by default so one has to load it.



[ Reply to This | # ]
Automate the download of YouTube videos in Safari 5.1
Authored by: hobbster on Mar 10, '12 10:49:38AM

Care to elaborate? I see the Filter Manager in iCab. But, then what? Thanks.



[ Reply to This | # ]
Automate the download of YouTube videos in Safari 5.1
Authored by: Bairnsfather on Mar 10, '12 02:27:16PM

Sure.

  1. Open iCab
  2. Tools > Filter Manager
  3. Check “Enable Filters” and Click the “Import…” button in the top left section (1).
  4. In the Open File dialog/sheet that appears navigate to the “Filters for Filter Manager” folder which came with the download of iCab
  5. Select whatever filters you want to, at least YouTube Video Download.icabfilter. (You can select more than one filter at a time to import multiple at once.)

That’s it! Nothing else to configure.

The only caveat I’ve found is that this filter doesn’t take effect on “channel” pages. I.e. user pages with their info and such. You have to be on a page that is specific to one video.

For example this user page will not show the links to download the videos you watch:
http://www.YouTube.com/CopsSayLegalizeDrugs

On these pages however, (with the filter correctly installed), you’ll see all the available resolutions and file formats:
http://www.youtube.com/watch?v=DnLaTnfwJVA
http://www.youtube.com/watch?v=xGt5uDN9znI



[ Reply to This | # ]
Automate the download of YouTube videos in Safari 5.1
Authored by: Bairnsfather on May 11, '12 09:17:56PM

I should add that if you visit YouTube via https, then you can easily edit the filter to work that way too.

1. Tools > Filter Manager
2. In section 2 "Filter URLs" just add an asterisk after the "p" in http to each of the three URLs.

They will look like this:
http*://www.youtube.com/*
http*://??.youtube.com/*
http*://youtube.com/*

Then no matter how you browse or land on YouTube iCab will show the available files.



[ Reply to This | # ]
Automate the download of YouTube videos in Safari 5.1
Authored by: daybrother on Mar 09, '12 07:17:07AM

I can't even save this. I keep getting a notice that I can't have a parameter after "pop over" at the end just before end tell.



[ Reply to This | # ]
Automate the download of YouTube videos in Safari 5.1
Authored by: philostein on Mar 09, '12 08:17:50AM

Are you on Safari 5.1? It has an iOS style floating window for downloads instead of a separate window.

Try this compiled script:

http://dl.dropbox.com/u/157506/Safari%20YouTube%20video%20downloader.scpt

Open it in AppleScript Editor and compare it to the one you're trying to save. Any differences?



[ Reply to This | # ]
Automate the download of YouTube videos in Safari 5.1
Authored by: g7 on Mar 12, '12 09:02:34AM
Note that this script works only when you use the English language. If you have a different language set in the International preferences, you need to modify the script, using the correct menu item names and window titles, for example:
set _downloadButton to item 1 of (every button whose description = "Pobieranie") of tool bar 1 of window _windowName
tell menu item "Aktywność" of menu "Okno" of menu bar item "Okno" of menu bar 1 to click
tell outline 1 of scroll area 1 of window "Okno aktywności"

...and so on.

[ Reply to This | # ]