Question

The following code has two lists: myURLs and myIMGs. I want to loop through the sites listed in myURLs, take a screenshot, then save them in my Shared folder as the file name listed in myIMGs. So, google.com's image will be saved as "google.jpg", and so forth. The list in myIMGs corresponds to the list in myURLs.

This is what I have, but it doesn't quite work. It loads the first URL from myURLs, takes a screenshot of it, but then loops through each file name in myIMGs and saves that one screenshot with all those various file names. Then, it loads the next URL in myURLs and does the same thing. How do I set the file names in the myIMGs list to correspond to the URLs being loaded from myURLs? I'm having trouble with the nested loops.

set myURLs to {"https://google.com", "https://wikipedia.org", "https://bing.com", "https://apple.com"}

set myIMGs to {"google.jpg", "wikipedia.jpg", "bing.jpg", "apple.jpg"}

-- Sets settings of Safari without interfering with user's settings.
repeat with myURL in myURLs
tell application "Safari"
    set myDoc to front document
    set windowID to id of window 1
    do JavaScript ("window.open('" & myURL & "','_blank','titlebar=0');") in document 1
    close window id windowID
    set the bounds of the front window to {0, 20, 915, 812}
    delay 5
end tell

-- Take screenshot, crop and save to Shared folder
repeat with myIMG in myIMGs

    do shell script "screencapture -o -l$(osascript -e 'tell app \"Safari\" to id of window 1') /Users/Shared/" & myIMG
    set this_file to "Macintosh HD:Users:Shared:" & myIMG
    try
        tell application "Image Events"
            -- start the Image Events application
            launch
            -- open the image file
            set this_image to open this_file
            -- get dimensions of the image
            copy dimensions of this_image to {W, H}
            -- Crops off the Safari header
            crop this_image to dimensions {W, H - 50}
            -- save the changes
            save this_image with icon
            -- purge the open image data
            close this_image
        end tell
    end try
end repeat
end repeat
Was it helpful?

Solution

You don't want to use two nested Repeat loops... you want to repeat and increment the variable n and then use item n of each list. Here's the changed code... tested and works as expected:

set myURLs to {"https://google.com", "https://wikipedia.org", "https://bing.com", "https://apple.com"}

set myIMGs to {"google.jpg", "wikipedia.jpg", "bing.jpg", "apple.jpg"}

-- Sets settings of Safari without interfering with user's settings.
repeat with n from 1 to (count myURLs)
    set myURL to item n in myURLs
    tell application "Safari"
        set myDoc to front document
        set windowID to id of window 1
        do JavaScript ("window.open('" & myURL & "','_blank','titlebar=0');") in document 1
        close window id windowID
        set the bounds of the front window to {0, 20, 915, 812}
        delay 5
    end tell

    -- Take screenshot, crop and save to Shared folder
    set myIMG to item n in myIMGs

    do shell script "screencapture -o -l$(osascript -e 'tell app \"Safari\" to id of window 1') /Users/Shared/" & myIMG
    set this_file to "Macintosh HD:Users:Shared:" & myIMG
    try
        tell application "Image Events"
            -- start the Image Events application
            launch
            -- open the image file
            set this_image to open this_file
            -- get dimensions of the image
            copy dimensions of this_image to {W, H}
            -- Crops off the Safari header
            crop this_image to dimensions {W, H - 50}
            -- save the changes
            save this_image with icon
            -- purge the open image data
            close this_image
        end tell
    end try
end repeat

You could simplify more by doing things like programmatically setting the file name based off of the URL string, and then you'd only have to maintain one list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top