AppleScript:按名称在Safari中找到打开的选项卡并打开它

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

我正在寻找一种方法来查找是否从所有打开的选项卡打开了一个网页,如果是,请关注此选项卡。

我试过这段代码,但显然这根本不起作用。

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

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

更新:我找到了:http://protips.maxmasnick.com/applescript-to-find-fastmail-tabs-in-safari

tabs safari applescript
3个回答
1
投票

Find Safari Tabs with AppleScript找到了一个优秀的剧本。如果多个选项卡与用户输入文本匹配,则会显示匹配选项卡列表,并允许用户选择一个打开。它需要一些小的mod来使用Mojave,所以这里是更新的代码:

set question to display dialog ("Find Safari tab whose name includes:") default answer ""
set searchpat to text returned of question

tell application "Safari"
    --
    -- *** Step 1. get a list of all tabs that match "searchpat" ***
    set winlist to every window
    set winmatchlist to {}
    set tabmatchlist to {}
    set tabnamematchlist to {}
    repeat with win in winlist
        if (count of tabs of win) is not equal to 0 then # ignore spurious windows with no tabs
            set tablist to every tab of win
            repeat with t in tablist
                if (searchpat is in (name of t as string)) or (searchpat is in (URL of t as string)) then
                    set end of winmatchlist to win
                    set end of tabmatchlist to t
                    set end of tabnamematchlist to (id of win as string) & "." & (index of t as string) & ".  " & (name of t as string)
                end if
            end repeat

        end if
    end repeat
    --
    -- *** Step 2. open the desired matching tab ***
    if (count of tabmatchlist) = 1 then
        set whichtab to (item 1 of tabnamematchlist)
        my openTab(whichtab)
    else if (count of tabmatchlist) = 0 then
        display notification "No matches"
    else
        set whichtab to choose from list of tabnamematchlist with prompt "The following tabs match, please select one:"
        if whichtab is not equal to false then
            my openTab(whichtab)
        end if
    end if
end tell

on openTab(whichtab)
    tell application "Safari"
        set AppleScript's text item delimiters to "."
        set tmp to text items of (whichtab as string)
        set w to (item 1 of tmp) as integer
        set t to (item 2 of tmp) as integer
        set current tab of window id w to tab t of window id w
        set index of window id w to 1
        -- this code is an essential work-around to activate a specific window ID in Mojave
        tell window id w
            set visible to false
            set visible to true
        end tell
    end tell
end openTab

0
投票

我编写了一个快速的脚本,并允许您使用正则表达式来指定URL的任一部分或选项卡的标题。它还会记住最后一个searchterm,还可以列出您获得的所有选项卡。 :)你可以在这里找到它:MacScripter / How to effectively converting a list of list into a single list

HTH


0
投票

只需尝试找到其URL包含搜索模式的选项卡,并捕获错误。

property checkURL : "yahoo"
try
    tell application "Safari" to set theTab to first tab of window 1 whose URL contains checkURL
    display dialog "That site is loaded"
on error
    display dialog "That site is not loaded"
end try
© www.soinside.com 2019 - 2024. All rights reserved.