Scroll a bit in safari window, take screenshot repeat

Alec Jacobson

February 22, 2013

weblog/

Here's an applescript I used to make a series of screen captures of scrolling through a web page in safari:
tell application "Safari"
	activate
	set sc to 0
	repeat
		set thescript to "if((window.pageYOffset + window.innerHeight)<document.body.clientHeight)
		{
    	window.scrollBy(0,10); // horizontal and vertical scroll increments
		}"
		do JavaScript thescript in current tab of first window
		set imagePath to (path to desktop as text) & "screenCapture_" & my zero_pad(sc, 6) & ".png"
		delay 1.5
		do shell script "screencapture -o -mx -T0 " & quoted form of POSIX path of imagePath
		delay 0.5
		set sc to sc + 1
	end repeat
	
end tell

on zero_pad(value, string_length)
	set string_zeroes to ""
	set digits_to_pad to string_length - (length of (value as string))
	if digits_to_pad > 0 then
		repeat digits_to_pad times
			set string_zeroes to string_zeroes & "0" as string
		end repeat
	end if
	set padded_value to string_zeroes & value as string
	return padded_value
end zero_pad
Source