Open All Linked Safari Movies in QuickTime with AppleScript

When Writing Tools We [at Macworld] Use showed up in my RSS reader, I immediately opened the post in Safari to watch the video. Here’s what you’re treated to when you click on the mp4 link:

embedded-video-safari-thumb.png

Elegant grays? Sure. But a tiny box in a browser window isn’t the ideal venue to watch a movie. Get around this situation by copying the movie’s URL, opening QuickTime Player, selecting ‘Open URL’ from the File menu and pasting the video in. Now you can resize the video to a watchable size and go on about your online business.

Here is an AppleScript to open the currently playing movie in Safari in QuickTime Player and save you a few clicks:

tell application "Safari"
	set nowPlay to URL of document 1
	close current tab of window 1
end tell

tell application "QuickTime Player"
	getURL nowPlay
	activate
end tell

Note the “close current tab” command in line three. I can’t see a reason to keep the movie tab open after QuickTime takes over, but if you can, just remove the line and the script will still work fine.

But this situation can be automated even further. What if, instead of actually opening the movie in a webpage before sending it to QuickTime, we could automatically search out the movie link and open it from that alone? And what if, moreover, the page contains links to multiple videos? Could we not open each linked movie in its own little player? Think of the time we’d save!

tell application "Safari"
	set link_count to (do JavaScript "document.links.length" in document 1)

	set myLinks to {}
	repeat with i from 0 to (the link_count - 1)
		set this_URL to (do JavaScript "document.links[" & (i as string) & "]" in ¬
			document 1)
		if this_URL ends with ".mpg" or this_URL ends with ¬
			".mpeg" or this_URL ends with ".mp4" or this_URL ends with ¬
			".wmv" or this_URL ends with ".mpeg4" or this_URL ends with ¬
			".avi" or this_URL ends with ".flv" or this_URL ends with ".mov" then
			set this_host to (do JavaScript ¬
				"document.links[" & (i as string) & "].host" in document 1)
			if this_host is "" then
				set this_URL to the parent_URL & this_URL

			end if
			set the end of the myLinks to this_URL
		end if
	end repeat

	repeat with i from 1 to number of items in the myLinks
		set the image_URL to item i of the myLinks
	end repeat
end tell

tell application "QuickTime Player"
	getURL myLinks
end tell

This script borrows heavily from Obi Kwiet’s amazing Open Linked Images in Tab script (it’s the second one down on the page). Out of the box it will support mpegs, mpeg4s and wmvs. If you want to add other formats, just insert

or this_URL ends with ".VidExtension"

at the appropriate place. In QuickTime preferences, make sure you have “Open movies in new players” checked!

If you don’t want to mess with Script Editor, you can download Send Linked Movies to QuickTime precompiled and run it from your script menu. If it doesn’t work for you or you see some way to make it more efficient, please don’t hesitate to let me know in the comments.

1 Response to Open All Linked Safari Movies in QuickTime with AppleScript

  1. Thank you so much for your help! It worked fine but I still have the problem: Quick Time wouldn’t open any URL with a Video File popping up an error message “invalid URL”. I have tried saving the url as Quicktime Movie. That would open for a few minutes than crash. Save as stream wouldnt work at all. Opening the same url in VLC wouldn’t play at all. Do you know any solution? Thanks in advance.