【问题标题】:AppleScript : find open tab in safari by name and open itAppleScript:按名称在 safari 中找到打开的选项卡并打开它
【发布时间】:2021-02-08 21:57:49
【问题描述】:

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

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

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

【问题讨论】:

  • PS 我也试过这个:告诉应用程序“Safari”将当前选项卡设置为 URL 包含的选项卡(“yahoo.com" 的内容”)结束告诉
  • 我发现它http://protips.maxmasnick.com/applescript-to-find-fastmail-tabs-in-safari
  • 该示例中的方法非常麻烦且没有必要。

标签: tabs safari applescript


【解决方案1】:

Find Safari Tabs with AppleScript 找到了一个出色的脚本。如果多个选项卡与用户输入的文本匹配,它会显示匹配选项卡的列表,并允许用户选择一个打开。 它需要几个小模块才能与 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

【讨论】:

  • 有没有办法让该标签在 safari 中完全聚焦。执行此搜索后,它实际上不允许我在不点击页面的情况下与之交互。
  • @David 以上对我来说仍然有效,就像在 Catalina 中一样。底部到set visible to false 和回到true 的行用于解决MacOS 中的一个错误,该错误在索引设置为1 时不会激活窗口(带到前面)。尝试在该部分插入一个小延迟,看看它是否会有所不同。
  • 我有一个相关的问题 - 我在使用这个脚本时遇到的问题 - apple.stackexchange.com/questions/432370/…
【解决方案2】:

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

HTH

【讨论】:

  • 这看起来是一个很棒的脚本,但最好将它的副本与 URL 一起放在这里。不幸的是,链接站点上存在文本编码问题,因此无法从那里复制/粘贴脚本。例如,在函数binarySearchinCumSum 中,我看到else if value ≤ item 1 of o's lst thenrepeat until (value ≤ item
【解决方案3】:

只需尝试查找其 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    • 2023-02-14
    相关资源
    最近更新 更多