编写Safari脚本以打开10个URL,然后每15秒旋转一次

时间:2019-06-07 12:48:58

标签: safari applescript

我需要能够在Safari中打开大约10个URL,然后轮流浏览它们,并在每个URL上暂停约15秒钟。我还希望将Safari窗口最大化。

我使用简单的Javascript尝试了此操作,但它会导致Windows每次刷新,这令人分心。因此,我认为带Safari的AppleScript将是一种“更清洁”的方法

这就是我的开始:

tell application "Safari"
activate
tell window 1
    make new tab with properties {URL:"http://news.yahoo.com"}
    make new tab with properties {URL:"http://news.google.com"}
    make new tab with properties {URL:"http://www.macintouch.com"}
    set current tab to tab 1
    set numTabs to number of tabs
end tell
activate
tell window 1
    repeat with j from 1 to (count of tabs of window 1) by 1
        set current tab to j
        activate
        delay 5
    end repeat
end tell

结束告诉

2 个答案:

答案 0 :(得分:1)

此AppleScript代码适用于使用最新版本的macOS Mojave的我。

请确保填写其余7个URL。我认为以下代码可以满足您的需求。

tell application "Safari"
    activate
    if not (exists of window 1) then make new document
    tell window 1
        set startingTabForRotating to make new tab with properties {URL:"http://news.yahoo.com"} -- URL 1
        make new tab with properties {URL:"http://news.google.com"} -- URL 2
        --make new tab with properties {URL:"something.com"}
        --make new tab with properties {URL:"something.com"}
        --make new tab with properties {URL:"something.com"}
        --make new tab with properties {URL:"something.com"}
        --make new tab with properties {URL:"something.com"}
        --make new tab with properties {URL:"something.com"}
        --make new tab with properties {URL:"something.com"}
        set endingTabForRotating to make new tab with properties {URL:"http://www.macintouch.com"} -- URL 10

        set current tab to startingTabForRotating

        delay 1
        tell application "System Events"
            if exists of UI element 3 of window 1 of application process "Safari" then
                click UI element 3 of window 1 of application process "Safari" -- Maximizes Safari
            end if
        end tell

        repeat until current tab is endingTabForRotating
            delay 15
            set current tab to tab ((index of current tab) + 1)
        end repeat
    end tell
end tell

答案 1 :(得分:1)

我对 @wch1zpink 采用了稍微不同的方法。在下面的脚本中,您可以编辑属性URLs并将其添加到属性property URLs : {"https://google.com", ¬ "https://dropbox.com", ¬ "https://imdb.com", ¬ "https://bbc.co.uk", ¬ "foobar blah blah", ¬ "https://disney.com"} property errorPage : "safari-resource:/ErrorPage.html" property time : 15 -- time (seconds) between tab switches tell application "System Events" to set screenSize to ¬ the size of scroll area 1 of process "Finder" tell application "Safari" make new document with properties {URL:the first item in the URLs} tell the front window set W to it repeat with URL in the rest of the URLs make new tab with properties {URL:URL} end repeat set the bounds to {0, 0} & the screenSize activate tell (a reference to the current tab) to repeat delay my time if not (W exists) then return set N to the number of tabs in W try set contents to tab (index mod N + 1) in W end try if its URL = errorPage then close end repeat end tell end tell 中,该属性将定义要打开的选项卡,同时将它们收集在脚本顶部并与代码主体分开。您可以根据需要提供无用的URL,因为脚本会妥善处理这些URL。

我还选择了最大化窗口而不是全屏显示,但是如果您更喜欢全屏显示,那么这样做很容易,我在最后添加了一个代码段以演示一种简洁的方法为此。

time

您可以设置的另一个属性是set the bounds to {0, 0} & the screenSize ,它定义了每个选项卡上暂停的时间(以秒为单位),目前设置为15秒。这些选项卡依次旋转,但是会连续旋转,直到关闭窗口。所有带有胡扯URL的选项卡都会在聚焦之前立即关闭,因此实际上过渡到下一个明智的选项卡是无缝的,并且您不会注意到关闭。

您仍然可以按需打开新标签页(在15秒的窗口内),并且可以手动切换到所需的任何标签页。进度将从您选择用作新起点的任何选项卡继续。

全屏

当前,脚本将窗口的大小设置为屏幕上可用的最大区域,在现代版本的macOS中,应将菜单栏和停靠点考虑在内,而不会使其模糊。

如果您希望使用全屏模式,则可以替换以下行:

tell application "System Events" to set value of attribute ¬
    "AXFullScreen" of front window of process "Safari" to true

与此:

tell application "Safari"

严格来说,应该将Safari块分成两个部分,尝试将这行放在language_from_string(str:list) -> HarfBuzz.language_t块之外。从第一个方块出来以执行全屏模式;然后重新进入第二个街区。但是,如果您不这样做,Safari也不会消失,因此,只要您知道这将被认为是懒惰且较差的形式,那么将一条线换成另一条线的简单剪切粘贴就足够了。