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!


  • http://haiku-os.org/ Stanley

    Cool, thanks for the detailed and informative script recipe. AppleScript can be idiosyncratic and esoteric, but this guide shows how easy and flexible the language can be.

    • http://www.desktopcurtain.co.uk Desktop Curtain

      http://www.desktopcurtain.co.uk

      Free mac osx applescript application to hide all your desktop icons at a click of a button.

  • Nick

    Incredibly useful. Thanks!

  • Corbanb

    This is fantastic. Should add to gist.github.com! I’d love to see this run the iOS Simulator and that screen grabs on the iphone simulator too!

    Thanks for the inspiration.

  • http://blog.jesusyepes.com jesus

    But, do you have full website screenshot?. Some time ago, i have a apple script that grabs the current url in safari and puts into a file. Then, using cutycapt y grab a screenshot of all websites in the file that contains the url. All is warking in the background, so i don’t need to put a delay that sometimes might not work.

    Using cutycapt is a best aproach, i think

  • http://www.lostmotionassembly.com Dre Jones

    Alternatively you can use Paparazzi from derailer.org. You can do a batch capture with a list of urls, and it allows you to grab a screenshot of the entire site. And did I mention it’s free?
    For even more streamlining, grab the TabLinks safari extension by Brett Terpstra. It’ll grab the url of every tab you have open and put it in a list that you can add to the clipboard. Paste into Paparazzi and boom: screen-grabbed goodness in seconds.
    Factor in Quicksilver and I can queue up a stack of screen grabs in 15 seconds AND save the URLs as a text file list for future reference.

    • http://www.coroflot.com/joshuajohnson Joshua Johnson

      Holy moses that’s the most amazing thing ever. Thanks!

      • http://www.lostmotionassembly.com Dre Jones

        Do not thank me, for I am but a vehicle; thank the awesome Brett Terpstra and Derailer.

        If there’s enough interest I might even do a screencast of the workflow.

  • http://www.leniel.net Leniel Macaferi

    Dre Jones,

    Paparazzi! is just what I was looking for…

    Thanks for sharing this fantastic piece of software.

    I wrote a blog post to showcase my findings regarding Paparazzi!’s batch capture functionality:

    http://www.leniel.net/2011/07/software-automate-site-screenshot.html

    Hope it helps.

  • DK

    Hi Guys,

    I have searched long and hard for a script that takes a window shot(Command + Shift + 4, then space on active window), to no avail.

    This would mean no user input required to crop the image down, as I would set the window size in the script.

    Any help would be greatly appreciated.

    Thanks,
    DK

    • Wolfienuke

      tell application “”
      activate
      set windowid to id of front window as string
      do shell script “screencapture -i -tjpg -l” & windowid
      end tell

      The “-l” option captures the window by the specified WindowID. I don’t think the command is required. Hope that is what you wanted. Also, add “-o” to the screencapture command line argument to remove the drop shadow from the screenshot.

  • Brian

    Is there anyway to have the screenshots save to a specific folder? For example, if I have a folder on my desktop called “Screenshots” how could I get the files to save there instead of my desktop itself?
    Thanks for for help.

    • Wolfienuke

      There are many ways to do that, but here is one…
      Add this tell to the top of the script:

      tell application “Finder”
      set picPath to ((POSIX path of (path to desktop)) & “ScreenShots/”) as string
      if not (exists folder picPath) then
      do shell script “mkdir -p ” & picPath
      end if
      end tell

      And edit the end of the script to this:

      tell application “Safari”
      activate
      set the URL of document 1 to currentUrl
      –Wait until it loads, then take a screenshot
      delay 5
      set picName to (fileNames & “-” & whichUrl & “.jpg”) as string
      do shell script “screencapture -tjpg ” & quoted form of (picPath & picName)
      end tell

  • Mel

    How could this script be combined with Automator Actions? For example: Could part of the sequence be Automator to set up the repeat, grab the list of files from TextEdit, then run part of the script, and back to Automator to increment the repeat. Your script worked for me, but I’m trying to learn to use both the Script Editor and Automator.

  • Scott Munn

    Great, just used this. Thanks!

  • http://macnoob.ru Apple Noob

    Thank you. Using this How-to tutorial I managed to create my first script. It wasn’t that hard.

theatre-aglow
theatre-aglow
theatre-aglow
theatre-aglow