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.
The Workflow
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
- 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
Step 1: Declare Some Variables
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.
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.
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
Repeat this for every URL repeat theCount times --some code here end repeat
Step 3: Grabbing a URL
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
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
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.
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
|
Putting it All Together
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
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
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
Alternative Version: Manually Select Grab Area
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.
do shell script "screencapture -i -tjpg " & quoted form of picPath
Conclusion
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!
Post a Comment