0

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!

  • If you're just trying to obtain copies of the images, you could consider calling curl instead of screencapture, which can download the images and save them for you. A basic call from the Terminal commandline would look like this: cd ~/Pictures/NYTimesPhotos; curl --remote-name-all --url https://archives.newyorker.com/rvimageserver/Conde%20Nast/New%20Yorker/1967_09_02/page[00001-00010].jpg, which would download 10 jpeg images. However, you'll have to read the manpage (man curl) in order to learn how to send credentials with curl to log in first. – CJK May 6 at 22:23
  • Thank you for your (quick) response. The reason I have decided to use the AppleScript/Safari method is because The New Yorker archive requires a user name/password to access these pages; but logging onto the site from a Safari session before calling the script I am able to display the pages successfully in a new Safari tab. (Actually, my original plan was to use a perl script, but I have run into the same problem as curl with the resultant downloaded files contain "403 - Forbidden: Access is denied"). – user13145 May 6 at 22:43
  • You can send credentials with curl to authorise your access. It’s in the man page. – CJK May 6 at 22:47
  • Could you show me how, please? The archive site appears to use JavaScript when you access the site archives.newyorker.com and click on the LOG IN button at the bottom of the screen. – user13145 May 6 at 22:57
  • Have a read of this answer. – CJK May 6 at 23:00
1

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.

enter image description here

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.

enter image description here

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

  • Thank you for the feedback, I like to idea of creating add-ons to safari. Unfortunately as I am looking for a method to automatically download 100+ pages of 52 magazines per year from 1926 to date on the face of it this method may require too much manual intervention. Regarding my script I listed above, I have a partial solution to my questions - firstly it is necessary to supply a directory and filename for screen capture, and I use a Perl script to extract an area from the saved file. – user13145 May 8 at 17:59
  • (As an aside) As you will see thanks to CJK, I use set thename to pagenumber & " 2,048×2,797 pixels" repeat while myexit = "false" if the document named (thename) exists then set myexit to "true" end if ....... end repeat Unfortunately, the document name on this site is not always the same as the value before 'pixels' varies. If there a way to include wildcards, use a regular expression, or statements such as "includes" or "contains" in "if the document named", in Applescript? – user13145 May 8 at 20:09
  • @user13145 Yes of course you could set a variable something like set partialText to "881 pixels" then your conditional statement could be something like if document (whose name contains partialText) exists then set myexit to "true". I use examples like that in my AppleScript code all the time. – wch1zpink May 10 at 17:13
  • Thank you so much for a gift of another verb! – user13145 May 12 at 20:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .