首页 > 解决方案 > AppleScript:使用带有特定 Safari WIN/TAB 索引的 JS

问题描述

我在特定的 Safari 窗口/选项卡中运行 JS 时遇到一些问题

  1. 特定的 Safari 窗口和选项卡以此为重点:

    set theUrl to "https://www.apple.com"
     tell application "Safari"
         repeat with thisWindow in (every window)
             repeat with thisTab in (every tab of thisWindow whose URL contains theUrl)
                 set (current tab of thisWindow) to thisTab
             end repeat
         end repeat
     end tell
    

但是,当我在之后运行脚本时(选项卡已经聚焦)但它不起作用,除非 我手动单击特定的 safari 窗口(我有一个显示了一些 safari 窗口的超狂野迷你器)

tell application "Safari"
    do JavaScript "document.getElementsByClassName('text-entry')[0].value=" & quoted form of myCaseNote in document 1
end tell

我也尝试使用这个(我的初始脚本来查找选项卡和窗口)并尝试引用选项卡 y 和窗口 x :

    tell application "Safari"
        --Variables
        set windowCount to number of windows
        set docText to ""
        --Repeat for Every Window
        repeat with x from 1 to windowCount
            set tabcount to number of tabs in window x
            --Repeat for Every Tab in Current Window
            repeat with y from 1 to tabcount
                --Get Tab Name & URL
                set tabName to name of tab y of window x
                set tabURL to URL of tab y of window x
                if tabURL contains "https://acmp.corp.apple.com" then
                    tell application "Safari"
                        tell window x
                            
                            set current tab to tab y
                        end tell
                    end tell
                end if
            end repeat
        end repeat
    end tell


tell application "Safari"
                set myLogOutMessage to do JavaScript "document.getElementsByClassName('logout-message')[0].innerHTML;" in tab y of window x
            end tell

但我有同样的问题

标签: javascriptapplescript

解决方案


尝试这个 :

set theUrl to "https://www.apple.com"
tell application "Safari"
    repeat with thisWindow in every window
        repeat with thisTab in (every tab of thisWindow whose URL contains theUrl)
            set current tab of thisWindow to thisTab
            tell thisTab
                set myLogOutMessage to do JavaScript "document.getElementsByClassName('logout-message')[0].innerHTML;"
            end tell
        end repeat
    end repeat
end tell

推荐阅读