Batch create PNG screenshots of webpages from *.webloc files

For one of my current projects at work, the creation of a well accessible electroplating patents database, I urgently needed a simple tool to create screenshots from webpages.

Of course, everybody knows the great «webkit2png» command line script from Paul Hammond and the related and very convenient GUI application Paparazzi!, which are both doing an excellent job.

But I had a special requirement: I needed to batch convert thousands of URLs saved in so called Web Internet Location files (*.webloc).

So I quickly wrote me a small AppleScript droplet that extracts the URLs from dropped *.webloc files and then creates the screenshots of the corresponding webpages in a chosen folder using webkit2png.

I frankly admit that my little script is a poor mans Paparazzi!, but it did the job just fine and saved me a lot of time.

If you also want to give it a try, then I invite you to download it right here:

WebKitty ¢ Create PNG screenshots of webpages from *.webloc files (ca. 37.2 KB)

WebKitty was successfully tested on Mac OS X 10.5.2 and works both on Intel & PowerPC based Macs.

To get started just drop a bunch of Web Internet Location files (*.webloc) onto the script icon and choose a folder to save the generated PNG images. You can also open WebKitty with a double-click, it will then ask you to enter a URL manually.

Thanks to Mac OS X Hints for teaching me how to read *.webloc URLs from the command line using the «strings» command:

strings /Users/martin/Desktop/www.apple.com.webloc/rsrc | grep http | sed ‘/^.http/s//http/’ | head -1

In my opinion WebKitty is really an excellent demonstration for AppleScript’s ability to nicely, quickly and conveniently combine certain technologies on the Mac:

AppleScript Droplet + Python code (webkit2png) + Unix scripting (strings command)

Important: Opening and saving the below script code in Script Editor won’t result in a usable AppleScript! That is because WebKitty internally relies on the «webkit2png» Python script, which is located inside its Application bundle. Therefor please download the complete script here.


-- created: 04.04.2008
-- tested on:
-- ¢ Mac OS X 10.5.2
-- ¢ Intel & PowerPC based Macs

-- This AppleScript creates PNG screenshots of webpages using the
-- well-known «webkit2png» script for Mac OS X from Paul Hammond.
-- <http://www.paulhammond.org/webkit2png/>
-- You can either drop Web Internet Location files (*.webloc)
-- onto the script icon or open the script with a double-click.
-- In the latter case you will be asked to enter a URL.
-- Moreover the script always asks you to choose a folder to
-- save the created PNG images.

-- Thanks to Mac OS X Hints for disclosing how to
-- read .webloc URLs from the command line using the «strings»
-- command.

-- This script is an excellent demonstration for AppleScript's
-- ability to nicely and conveniently combine certain technologies
-- on the Mac...

property mytitle : "WebKitty"

-- I am called when the user opens the script with a double-click
on run
	-- asking the user to provide a URL
	set weblocurl to my askforurl()
	-- asking the user to provide a folder path to
	-- save the created PNG images therein
	set folderpath to (choose folder) as Unicode text
	-- finally extracting the URLs from the *.webloc files
	-- and creating the PNG images
	try
		my webkit2png(weblocurl, folderpath)
	on error errmsg number errnum
		my dsperrmsg(errmsg, errnum)
	end try
end run

-- I am called when the user drops Finder items onto the script icon
on open droppeditems
	set weblocpaths to {}
	-- did the user drop any *.webloc files onto the script?
	repeat with droppeditem in droppeditems
		if (droppeditem as Unicode text) ends with ".webloc" then
			set weblocpaths to weblocpaths & (droppeditem as Unicode text)
		end if
	end repeat
	-- no, he or she didn't!
	if weblocpaths is {} then
		set errmsg to "You did not drop any *.webloc files onto me."
		my dsperrmsg(errmsg, "--")
	else
		-- asking the user to provide a folder path to
		-- save the created PNG images therein
		set folderpath to (choose folder) as Unicode text
		-- finally extracting the URLs from the *.webloc files
		-- and creating the PNG images
		repeat with weblocpath in weblocpaths
			try
				set weblocurl to my geturlfromwebloc(weblocpath)
				my webkit2png(weblocurl, folderpath)
			on error errmsg number errnum
				my dsperrmsg(errmsg, errnum)
			end try
		end repeat
	end if
end open

-- I am extracting the URL from a Web Internet Location file (*.webloc)
-- using the «strings» command
on geturlfromwebloc(weblocpath)
	set weblocpath to quoted form of ((POSIX path of weblocpath) & "/rsrc")
	set cmd to "strings " & weblocpath & " | grep http | head -n 1"
	set cmd to cmd as «class utf8»
	set weblocurl to do shell script cmd
	return weblocurl
end geturlfromwebloc

-- I am creating PNG images from a given URL in a given folder path
-- using the «webkit2png» script from Paul Hammond
-- <http://www.paulhammond.org/webkit2png/>
on webkit2png(weblocurl, folderpath)
	set mypath to (path to me) as Unicode text
	set pyscriptpath to mypath & "Contents:Resources:webkit2png.py"
	set cmd to "/usr/bin/python " & quoted form of (POSIX path of pyscriptpath) & space & "-D " & quoted form of (POSIX path of folderpath) & space & weblocurl
	set cmd to cmd as «class utf8»
	do shell script cmd
end webkit2png

-- I am asking the user to provide a URL
on askforurl()
	tell me
		activate
		display dialog "Please enter a URL:" & return & "(e.g. http://macscripter.net)" default answer "http://" buttons {"Cancel", "Enter"} default button 2 with title mytitle
		set dlgresult to result
	end tell
	if text returned of dlgresult is "" then
		my askforurl()
	else
		set weblocurl to text returned of dlgresult
		if weblocurl does not start with "http://" then
			set weblocurl to "http://" & weblocurl
		end if
		return weblocurl
	end if
end askforurl

-- I am displaying error messages
on dsperrmsg(errmsg, errnum)
	set errmsg to "Sorry, an error occured:" & return & return & errmsg & " (" & errnum & ")"
	tell me
		activate
		display dialog errmsg buttons {"OK"} default button 1 with icon stop with title mytitle
	end tell
end dsperrmsg