如何使用 applescript 通过 url 关闭 safari 选项卡

问题描述 投票:0回答:3

我正在寻找一种通过 url 关闭 safari 中特定选项卡的方法。 我可能会扩展这样的内容:

tell application "Safari"
    repeat with t in tabs of windows
        tell t
            if name starts with "google.com" then close
        end tell
    end repeat
end tell

这里的问题是我还没有找到一种方法来获取选项卡的 url 值并据此关闭它们。

macos tabs safari applescript
3个回答
3
投票

您可以根据活动选项卡从 Safari 获取 URL。

tell application "Safari"
    set w to first window
    set t to current tab of w
    display dialog (URL of t as string)
end tell

然后你可以像这样迭代每个选项卡/页面:

tell application "Safari"
    set windowCount to number of windows
        repeat with x from 1 to windowCount
            set tabCount to number of tabs in window x
            repeat with y from 1 to tabCount
                set thistab to tab y of window x
                set foo to URL of thistab
                if foo is not equal to "bar" then close thistab
           end repeat
       end repeat
end tell

1
投票

这是另一种方法:

set closeURLs to {"http://www.yahoo.com", "http://www.apple.com"}

repeat with theURL in closeURLs
    tell application "Safari" to close (every tab of every window whose URL contains (contents of theURL))
end repeat

0
投票

旧答案对我不起作用。这就是我的答案。

set closeURLs to {"google", "youtube"}

tell application "Safari"
    set windowNumber to 1
    repeat the number of windows times
        repeat with theURL in closeURLs
            close (every tab in window windowNumber whose name contains theURL)
        end repeat
        set windowNumber to windowNumber + 1
    end repeat

end tell

参考文献

© www.soinside.com 2019 - 2024. All rights reserved.