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

时间:2013-07-01 13:49:09

标签: macos tabs safari applescript

我正在寻找一种通过网址关闭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值并根据它关闭它们。

2 个答案:

答案 0 :(得分:3)

您可以根据活动标签获取Safari的网址。

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 :(得分: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