获取 Applescript 中特定选项卡的 window

Get window of certain tab in Applescript

所以我试图将名为 theWindow 的变量设置为 window,其中的选项卡 URL 包含 "google.com"

tell application "Google Chrome"
    set theTabs to a reference to (tabs of windows whose URL contains "google.com")
    set theTab to item 1 of theTabs
    set theWindow to window of theTab
end tell

但是,这不起作用,代码 returns:

Google Chrome got an error: Can’t get window of tab id 384 of window id 383." number -1728 from window of tab id 384 of window id 383

我不使用 Chrome,因此您可能需要进行转换,但这是使用 Safari 时的转换方式。

tell application "Safari"
    set sst to "nikon"
    
    repeat with w in windows
        if exists (tabs of w whose URL contains sst) then
            set wix to index of w
            set wid to id of w
            exit repeat
        end if
    end repeat
end tell
display dialog "Window: " & wix & return & "Window ID: " & wid

它循环打开 windows 并检查它的任何选项卡是否有匹配的 URL。如果至少有一个,那么它会提供那个 window 的索引和 ID。显然,如果多个选项卡匹配搜索词,那么结果将受到影响。