Tim Vavra
Tim Vavra

Reputation: 537

Applescript run Safari in background

I have an applescript that is now doing what I want it to do: Open a specific URL, close the page and wait 2 seconds then do it again. The script works.

The problem is that when I run this on my machine, and I am trying to do something else at the same time, the Safari windows keeps popping up. I would really like to run this in the background so that I can continue to work on whatever I am doing.

The code I have is:

set theURL to "https://sites.google.com/site/whatever/"

repeat 100 times
    tell application "Safari"
        activate
        try
            tell window 1 to set current tab to make new tab --with properties {URL:theURL}
            set URL of document 1 to theURL
        on error
            open location theURL
        end try
        delay 2
        try
            close window 1
        end try
    end tell

    tell application "Safari"
        quit
    end tell

    delay 1
end repeat

Anyone have a solution to this one?

Upvotes: 1

Views: 2605

Answers (1)

adayzdone
adayzdone

Reputation: 11238

Safari is coming to the front because you are activating it within the loop.

repeat 100 times
    tell application "Safari"
        if not (exists document 1) then reopen
        set URL of document 1 to "https://sites.google.com/site/whatever/"
        delay 2
        close window 1
        quit
    end tell
    delay 1
end repeat

EDIT

set myURLs to {"https://www.google.com/", "http://www.yahoo.com/"}

repeat with aUrl in myURLs
    repeat 100 times
        tell application "Safari"
            if not (exists document 1) then reopen
            set URL of document 1 to aUrl
            delay 2
            close window 1
            quit
        end tell
        delay 1
    end repeat
end repeat

Upvotes: 3

Related Questions