Applescript在后台运行Safari

时间:2013-08-12 15:47:46

标签: applescript

我有一个AppleScript,现在正在做我想要它做的事:打开一个特定的URL,关闭页面并等待2秒然后再做一次。该剧本有效。

问题在于,当我在我的机器上运行它,并且我正在尝试同时执行其他操作时,Safari窗口会不断弹出。我真的很想在后台运行它,以便我可以继续处理我正在做的事情。

我的代码是:

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

任何人都有解决方案吗?

1 个答案:

答案 0 :(得分:3)

Safari正走在前面,因为你正在循环中激活它。

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

修改

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