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" & i & ".jpg" set outfile to "page00" & i & ".png" else if i is less than 100 then set pagenumber to "page00000" & i & ".jpg" set outfile to "page0" & i & ".png" else set pagenumber to "page0000" & i & ".jpg" set outfile to "page" & i & ".png" end if end if (* generate URL *) set html to "https://archives.newyorker.com/rvimageserver/Conde%20Nast/New%20Yorker/1967_09_02/" & 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 & " 2,048×2,797 pixels" set othename to pagenumber & " 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 " & window_id & " " & 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!