Calling Screencapture of a Web Page in AppleScript As a newcomer to AppleScript, it is quite possible that I am missing something obvious, but I would appreciate feedback!  My intention is to use Safari to load a series of images from an issue of The New Yorker and save as a sequence of png files.

Here's my attempt (sorry for the strange look to this but I had problems with copy/paste into the 'body' of this message due to the comments in my code) :

set i to 1
repeat 10 times
(*
format pagenumber to 11 characters
format outfile    to  7 characters *)
if i is less than 10 then
    set pagenumber to "page000000" &amp; i &amp; ".jpg"
    set outfile to "page00" &amp; i &amp; ".png"
else
    if i is less than 100 then
        set pagenumber to "page00000" &amp; i &amp; ".jpg"
        set outfile to "page0" &amp; i &amp; ".png"
    else
        set pagenumber to "page0000" &amp; i &amp; ".jpg"
        set outfile to "page" &amp; i &amp; ".png"

    end if
end if
(* generate URL *)
set html to "https://archives.newyorker.com/rvimageserver/Conde%20Nast/New%20Yorker/1967_09_02/" &amp; pagenumber
tell application "Safari"
    if not (exists document 1) then reopen
    (* load URL *)
    tell current tab of window 1 to set URL to html
    (* get the id of that window        *)
    set window_id to id of window 1
    (* when web page has finally loaded, the resultant document will consist of the page number plus 2,048×2,797 pixels, or 2,048×2,881 pixels ie   "page0000002.jpg 2,048×2,797 pixels" or "page0000010.jpg 2,048×2,881 pixels"    *)
    set thename to pagenumber &amp; " 2,048×2,797 pixels"
    set othename to pagenumber &amp; " 2,048×2,881 pixels"
    (*   wait for page to load see  https://apple.stackexchange.com/questions/319986/applescript-help-wait-for-safari-page-to-load *)
    set myexit to "false"
    repeat while myexit = "false"
        if the document named (thename) exists then
            set myexit to "true"
        end if
        if the document named (othename) exists then
            set myexit to "true"
        end if
    end repeat
    delay 2
end tell
(* caputure the image element of window and save to outfile *)
do shell script ("screencapture -R 518,111,640,877 -l " &amp; window_id &amp; " " &amp; outfile)
delay 2
(*  increment count *)
set i to i + 1 
end repeat


Everything seems to work as expected until I attempt to capture the screen to an output file when I encounter two problems:

~ no output files are created (the same command in terminal works)

~ the -R option to "screencapture" to select part of the window isn't working

Thank you, for any advice!
Whenever I can I usually try to only use AppleScript and shell script but every now and then I find a hidden gem in Automator. I simply created a new Automator workflow and added three actions to it... "Get Current Webpage From Safari","Get Contents Of Webpages", and "Save Images From Web Content". 

This works for me using the latest version of macOS Mojave.



Just save the Automator workflow as an application or as a service or dictation command...etc.

For this project, my choice would be to simply save the Automator workflow in folder... /Users/YOUR SHORT NAME/Library/Scripts/Applications/Safari (if the Safari folder does not exist, you can simply create the Safari folder during the process of saving your Automator workflow) Saving the file to that location, will make it so that anytime you are using Safari, you can run the workflow from your Scripts menu bar.



If the Scripts menu is not already in your menu bar, you can enable that in the General Preferences menu of Script Editor.app