AppleScript: Automatically Create Screenshots from a List of Websites

It’s been quite a while since we’ve had any fun with AppleScript so today we’re going to build a super basic script that automatically reads a list of URLs and turns them into screenshots.

If you’re new to AppleScript, be sure to read our introduction and advanced articles! We’ll be explaining things as we go but everything will make a lot more sense once you do your homework.

The Workflow

As a full-time blogger, it’s quite often the case that I have to put together a list of well-designed websites or some other post that requires me to post screenshots of anywhere from fifty to one hundred web pages. This is an exhausting process!

Since I’m lazy and love to sit back and watch my computer do my work for me, I wrote a simple script to handle the process. Basically, we’ll be using a modified version of a TUAW script to take a screenshot and adding in some of our own code to progressively launch a list of links. Let’s get started!

Algorithm

An algorithm is just a fancy word for the general path of our script. Before you write a single line of code, you should always outline the steps that you’ll need to take to reach your goal. For this post, ours is quite simple:

  • Create a list of URLs in Text Edit (manually)
  • Count the URLs in TextEdit
  • Grab the first URL
  • Open the first URL in Safari
  • Take a screenshot
  • Repeat for the rest of the URLs

That’s not so bad is it? We should be able to knock this out in no time.

Step 1: Declare Some Variables

One of the first steps I like to take when coding is to think about the variables that I’ll need and declare them. For instance, here I know I’ll need one counter that will help me cycle through the URLs, a variable to hold the total number of URLs and a variable to hold a customizable file name for the resulting files.

To this end, we’ll declare our counter and filename first. I called the counter “whichUrl” because it will essentially be keeping track of which URL we’re on throughout the script. Notice that I’ll be using comments quite liberally throughout the script, this is a good practice to get into.

1
2
3
--Variables
set whichUrl to 1
set fileNames to "webshot"

Next up, we need to create a tell block that grabs the number of URLs from TextEdit. If we put each URL on a line of its own, AppleScript will see each URL as a paragraph, so we simply tell it to set our variable equal to the number of paragraphs in the front document. This effectively counts our URLs for us.

1
2
3
4
--Get Number of URLs
tell application "TextEdit"
	set theCount to the count of paragraphs of front document
end tell

Step 2: Create a Repeat Block

At this point of the script, we’ll have everything we need to start grabbing URLs and taking screenshots. What we need to do is create a loop of commands that will repeat for every URL. Since we took the number of URLs and put it into our “theCount” variable, we can tell AppleScript to repeat that many times.

1
2
3
4
--Repeat this for every URL
repeat theCount times
	--some code here
end repeat

Step 3: Grabbing a URL

The first step we want to do within our repeat block is to grab the first URL and put it in a variable so we can open it in Safari later. To do this, we take advantage of our whichURL variable, which is initially set to one.

1
2
3
4
5
6
7
8
9
--Repeat this for every URL
repeat theCount times
 
	--Get the next URL
	tell application "TextEdit"
		set currentUrl to paragraph whichUrl of front document as text
	end tell
 
end repeat

As you can see, we told TextEdit to grab the contents of the first paragraph and throw it into the variable “currentURL”. Admittedly, my variables are a bit confusing and could be named a bit better! Feel free to change them to something that makes more sense to you.

Step 4: Open the URL in Safari

Now that we have the URL set to a variable, it’s time to open a Safari window with the URL set to that value. Basically we just create a tell block and set the URL of document one to the variable we created in step three.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
--Repeat this for every URL
repeat theCount times
 
	--Get the next URL
	tell application "TextEdit"
		set currentUrl to paragraph whichUrl of front document as text
	end tell
 
	--Open the URL in Safari
	tell application "Safari"
		activate
		set the URL of document 1 to currentUrl
	end tell
end repeat

Step 5: Take the Screenshot

One of the struggles that I had with creating this script is that the page wasn’t always fully loaded when the screenshot was taken, so the resulting file was either a blank Safari window or a half-loaded web page.

The simple solution is to insert a slight delay. If your Internet connection is slow, you might want to increase this value. After that, we then take and save the screenshot. The saving is done by determining the path to your desktop, then applying the filename variable we set up before plus a number on the end to indicate which URL it was. So the third URL should come out as “webshot-3.jpg” on your desktop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
--Repeat this for every URL
repeat theCount times
 
	--Get the next URL
	tell application "TextEdit"
		set currentUrl to paragraph whichUrl of front document as text
	end tell
 
	--Open the URL in Safari
	tell application "Safari"
		activate
		set the URL of document 1 to currentUrl
 
		--Wait until it loads, then take a screenshot
		delay 15
		set saveToPath to ((POSIX path of (path to desktop)) & fileNames & "-" & whichUrl & ".jpg") as string
		do shell script "screencapture -tjpg " & quoted form of saveToPath
	end tell
end repeat

Step 6: Increase the Counter

The last thing we need to do is increase the “whichURL” variable by one. This will cause the script to jump to URL #2 in your TextEdit Document and make sure the filename gets a “-2” on the end of it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
--Repeat this for every URL
repeat theCount times
 
	--Get the next URL
	tell application "TextEdit"
		set currentUrl to paragraph whichUrl of front document as text
	end tell
 
	--Open the URL in Safari
	tell application "Safari"
		activate
		set the URL of document 1 to currentUrl
 
		--Wait until it loads, then take a screenshot
		delay 5
		set picPath to ((POSIX path of (path to desktop)) & fileNames & "-" & whichUrl & ".jpg") as string
		do shell script "screencapture -tjpg " & quoted form of picPath
	end tell
 
	--Increase the counter for next time
	set whichUrl to whichUrl + 1
end repeat

Putting it All Together

That’s it! Our script is nice and short and will save you loads of time if you ever need to take a screenshot of a webpage. Here’s the full script, feel free to copy and paste it right into AppleScript Editor.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
--Variables
set whichUrl to 1
set fileNames to "webshot"
 
--Get Number of URLs
tell application "TextEdit"
	set theCount to the count of paragraphs of front document
end tell
 
--Repeat this for every URL
repeat theCount times
 
	--Get the next URL
	tell application "TextEdit"
		set currentUrl to paragraph whichUrl of front document as text
	end tell
 
	--Open the URL in Safari
	tell application "Safari"
		activate
		set the URL of document 1 to currentUrl
 
		--Wait until it loads, then take a screenshot
		delay 5
		set picPath to ((POSIX path of (path to desktop)) & fileNames & "-" & whichUrl & ".jpg") as string
		do shell script "screencapture -tjpg " & quoted form of picPath
	end tell
 
	--Increase the counter for next time
	set whichUrl to whichUrl + 1
end repeat

Running the Script

Before you test out the script, be sure to open TextEdit and copy in a list of links. You don’t need to save the document, just be sure that it’s the frontmost page in TextEdit and everything will work fine.

screenshot

The Requisite TextEdit Document

Also, the script works best when you have maximized Safari windows, so before you begin, open a new window in Safari and stretch it as big as it’ll go. After that, you’re all set to hit play and watch the magic happen!

Room for Improvement

As I said, this script saves me loads of time and I love using it, but I fully admit that it could stand to be improved. For starters, it’s limited to the visual portion of the page and doesn’t take a screenshot of the entire website. LittleSnapper works great for this but last I checked there wasn’t any AppleScript support (get on that RealMac).

Further, I’m not sure how to snap only preset, specific coordinates of the screen, so this script just grabs your entire screen, browser chrome, menu bar and all (check below for an alternate version). This is definitely undesirable but I always just automate the cropping as well. You could automate the crop with AppleScript but I find that it’s easier just to use Photoshop and the following steps:

  • Open one of the screenshots
  • Create a new action and hit record
  • Crop the image
  • Close/save the image
  • Stop recording
  • Go to File>Automate>Batch and run the action on your entire folder of screenshots

This is definitely a little annoying but running the script and action setup are a one-time process that can just be quickly run every time you use them. I’d definitely love to see some talented scripters make suggestions for how to improve this to be a little less clunky!

Alternative Version: Manually Select Grab Area

If you really hate the cropping process, you can actually change the script so that you manually select the portion of the screen to capture. Everything else will stay automated, you simply have to draw a box for each capture to take place.

To accomplish this, change the shell script line near the bottom to the one shown below. All I added was a “-i” after the “screencapture” command.

1
do shell script "screencapture -i -tjpg " & quoted form of picPath

Conclusion

Taking screenshots of a huge list of websites for a roundup post can be time consuming and mind-numbing. Fortunately, with AppleScript and a little ingenuity, you can automate the entire process. Simply create your list of URLs then sit back and enjoy the show.

Leave a comment below if you have any suggestions for improving the script. Also, be sure to let me know what other tasks you find frustrating and wish you could automate, if I think it will help a lot of people, I’ll whip up a tutorial!