【问题标题】:Scripting Safari to open 10 URLs and then rotate through through then about every 15 seconds编写 Safari 以打开 10 个 URL,然后大约每 15 秒旋转一次
【发布时间】:2019-10-22 23:35:01
【问题描述】:

我需要能够在 Safari 中打开大约 10 个 URL,然后在它们之间旋转,在每个 URL 上暂停大约 15 秒。我还希望 Safari 窗口最大化。

我用一个简单的 Javascript 进行了尝试,但它会导致窗口每次都刷新,这会让人分心。所以,我认为 AppleScript 与 Safari 将是一种“更干净”的方法

这是我开始的:

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

说完

【问题讨论】:

  • 您说,“我还希望将 Safari 窗口最大化。”,我们想知道您所说的“最大化”到底是什么意思?您的意思是 Full Screen view,(例如 Safari > View > Enter Full Screen),还是只是调整窗口大小以填充 Desktop 而仍然显示 菜单栏Dock,(假设它们没有设置为隐藏)?此外,您是只想通过选项卡旋转一次,还是重复旋转?

标签: safari applescript


【解决方案1】:

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

请务必填写剩余的 7 个网址。我认为以下代码可以实现您的目标。

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

【讨论】:

    【解决方案2】:

    我对 @wch1zpink 采取了稍微不同的方法。在下面的脚本中,您可以编辑并添加到属性URLs,它将定义要打开的选项卡,同时将它们收集在脚本的顶部并与代码的主体分开。如果您愿意,您可以提供无意义的 URL,因为脚本会优雅地处理这些 URL。

    我还选择最大化窗口而不是全屏,但是如果您更喜欢全屏,那么这很容易完成,我在末尾添加了一个 sn-p 来演示实现这一目标的干净方法。

    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
    

    您可以设置的另一个属性是time,它定义了在每个选项卡上暂停的时间(以秒为单位),目前设置为 15 秒。选项卡依次旋转,但在您关闭窗口之前连续循环。任何带有无意义 URL 的选项卡都会在成为焦点之前立即关闭,因此到下一个有意义的选项卡的过渡实际上是无缝的,您不会注意到这些关闭。

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

    全屏

    目前,脚本将窗口大小设置为屏幕上可用的最大区域,在现代版本的 macOS 中,应考虑菜单栏和停靠栏而不遮挡它们。

    如果您更喜欢全屏模式,那么您可以替换这一行:

    set the bounds to {0, 0} & the screenSize
    

    用这个:

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

    严格来说,您应该尝试将这条线放在tell application "Safari" 块之外,将Safari 块分成两部分;走出第一个街区以制定全屏模式;然后重新进入第二块。但如果你不这样做,Safari 不会死,所以只要你意识到这会被认为是懒惰和糟糕的形式,一个简单的剪切和粘贴将一行换成另一行就足够了。

    【讨论】:

    • 尽管 OP 对 AppleScript 来说显然是新的,但是,这显然是高级脚本技术有益的例子。 +1 我要做的唯一改变是,如果要在全屏视图中运行,我会将 set contents to tab (index mod N + 1) in W command 包装在 try statement 中,所以以便能够切换回桌面,同时将该 Safari 窗口留在全屏视图中并且不会出错。不在全屏视图中时没有必要。只是一些思考的食物。
    • 好主意@user3439894。我没有考虑到这一点。尽管全屏通常会带来一个问题,但如果用户确实切换回桌面,则大部分重复块都会受到对W 的任何引用的影响。事实上,它会导致脚本退出if not (W exists) 条件,try 块不会阻止。我现在想不出办法。有什么想法吗?
    • 在我的系统上它并没有将其从全屏中剔除,W 仍然存在;但是,当 W in 全屏并且切换回桌面时,当我提到的行上有 try statement 时,脚本仍然运行。当切换回全屏 W 时,tabs 继续明显地继续切换。
    • 好吧,谢谢你。没有收到有关编辑的提醒,但在您提到之后才看到它并且它就在那里,因此已被接受。 try 块现在就位。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2014-07-05
    • 1970-01-01
    • 1970-01-01
    • 2019-12-29
    • 2021-03-21
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多