在 Chrome 中使用 AppleScript 按标题关闭多个选项卡/窗口的可靠方法?

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

我想使用 AppleScript 关闭 Chrome 中具有特定标题的所有选项卡。我想出了这段代码,但感觉不太健壮:

tell application "Google Chrome"
    set tabsToClose to {}
    repeat with win in windows
        repeat with t in tabs of win
            set tabTitle to title of t
            if tabTitle is "New tab" then
                set end of tabsToClose to t
            end if
        end repeat
    end repeat

    repeat with tabToClose in reverse of tabsToClose
        close tabToClose
    end repeat
end tell
如果您关闭“较早”的选项卡/窗口,选项卡和窗口的

index

id
 都会发生变化。如果您有一个带有四个选项卡的窗口,并且您想要关闭索引为二和三的选项卡,则在关闭选项卡二后,选项卡三将重新编号为二(如果您现在执行“关闭选项卡三”,您实际上会关闭第四个选项卡,不应关闭)。

我解决这个问题的方法是循环浏览选项卡列表以反向关闭,但这感觉像是一个容易崩溃的黑客行为。还有更好的办法吗?

loops google-chrome indexing applescript
2个回答
0
投票
tell application "Google Chrome" repeat with win in windows close (tabs of win whose title is "New tab") end repeat end tell
    

0
投票
您甚至不需要重复循环。

tell application "Google Chrome" close (tabs of windows whose title is "New tab") end tell
    
© www.soinside.com 2019 - 2024. All rights reserved.