andrhamm
andrhamm

Reputation: 4411

AppleScript: How to loop through list of URLs one at a time to open & close a browser window

I'm trying to load a list of URLs in a browser one at a time. I don't care about any output, I just need the page to load, once finished loading completely, change the location to the next URL in the list. I've tried and given up accomplishing this in Automator. I'd use CURL but I don't know enough about passing authentication (several cookies must be present).

Is this possible with a simple ActionScript? Ideally, input would be a txt file with a line for each URL.

Update! This is working:

set thePath to (path to desktop as Unicode text) & "sites.txt"
set theFile to (open for access file thePath)
set theContent to (read theFile)
close access theFile

set theURLs to every paragraph of theContent

tell application "Safari"
    repeat with theURL in theURLs
        make new document
        set URL of front document to theURL
        delay 1
        repeat until ((do JavaScript "document.readyState" in front document) is "complete")
            delay 1
        end repeat
        close front document
    end repeat
end tell

Upvotes: 1

Views: 5150

Answers (1)

eykanal
eykanal

Reputation: 27017

This sets a list of URLs, loads each one, and waits until the document is ready to continue. Note that each URL has to be complete (i.e., http://cnn.com/ and not cnn.com) to load a page this way.

set myURLs to {"http://stackoverflow.com/", "http://cnn.com/", "http://nytimes.com/"}

tell application "Safari"
    make new document
    set myDoc to front document
    repeat with myURL in myURLs
        set URL of myDoc to myURL
        -- wait until page is loaded to open new page
        repeat until ((do JavaScript "document.readyState" in front document) is "complete")
            delay 1
        end repeat
    end repeat
end tell

Upvotes: 3

Related Questions