在 AppleScript 中获取活动浏览器窗口详细信息

JGC*_*JGC 0 applescript

目标:识别 macOS 中最近的浏览器窗口,并以 Markdown 链接的形式获取其活动选项卡的 URL 和标题。

它注定要用于从其他应用程序触发的 Alfred 工作流程,但现在我只是在脚本编辑器中调试它的核心。我打开了 Safari 和 Chrome,以及许多其他应用程序。通过调试,我看到它正确列出了所有打开的窗口,但它从不匹配任何一个if条件。作为进一步的证据,如果我只是tell application单独使用这些行,就会返回正确的结果。我确信这非常简单。

set output to ""
tell application "System Events"
    set appNames to name of every application process whose visible is true
    repeat with appName in appNames
        if (appName = "Google Chrome") then
            using terms from application "Google Chrome"
                tell application appName to set currentTabTitle to title of active tab of front window
                tell application appName to set currentTabUrl to URL of active tab of front window
            end using terms from
            set output to "[" & currentTabTitle & "](" & currentTabUrl & ")"
            exit repeat
        else if (appName = "Safari") then
            using terms from application "Safari"
                tell application appName to set currentTabTitle to name of front document
                tell application appName to set currentTabUrl to URL of front document
            end using terms from
            set output to "[" & currentTabTitle & "](" & currentTabUrl & ")"
            exit repeat
        end if
    end repeat
end tell
return output
Run Code Online (Sandbox Code Playgroud)

CJK*_*CJK 6

正如评论中所讨论的,您的脚本所做的假设是 AppleScript 将返回按最近获得焦点的应用程序排序的进程列表,但事实并非如此。

\n\n

但是,您可以使用 shell 命令按此顺序检索应用程序名称列表lsappinfo metainfo。通过一些附加命令来隔离感兴趣的信息并清理文本:

\n\n
lsappinfo metainfo \\\n    | grep bringForwardOrder \\\n    | grep -E -o \'"[^"]+"\' \\\n    | tr -d "\\""\n
Run Code Online (Sandbox Code Playgroud)\n\n

生成一个漂亮、可读、有序的应用程序列表,其中最后一项的最后活动时间比下面的一项要晚:

\n\n
Google Chrome\nScript Editor\nAtom\nMessages\nWhatsApp\nFinder\nSafari\nScript Debugger\nWebTorrent\n
Run Code Online (Sandbox Code Playgroud)\n\n

对此进行测试,当我切换到Script Editor,然后再次运行 shell 命令时,返回的列表是:

\n\n
Script Editor\nGoogle Chrome\nAtom\nMessages\nWhatsApp\nFinder\nSafari\nScript Debugger\nWebTorrent\n
Run Code Online (Sandbox Code Playgroud)\n\n

由于您只对辨别两个特定应用程序(即SafariGoogle Chrome)之间的顺序感兴趣,因此 shell 命令可以稍微简化为:

\n\n
lsappinfo metainfo | grep -E -o \'Safari|Google Chrome\' | head -1\n
Run Code Online (Sandbox Code Playgroud)\n\n

它将返回一个名称,即当前处于活动状态或最近获得焦点的浏览器;或者,如果两个浏览器都没有运行,则为空字符串。

\n\n

将其合并到您的 AppleScript 中,并稍微清理一下脚本:

\n\n
property nil : ""\n\nset [currentTabTitle, currentTabUrl] to [nil, nil]\n\nset cmd to "lsappinfo metainfo | grep -E -o \'Safari|Google Chrome\' | head -1"\nset frontmostBrowser to do shell script cmd\n\nif the frontmostBrowser = "" then return nil\n\nif the frontmostBrowser = "Google Chrome" then\n\n    tell application "Google Chrome" to tell \xc2\xac\n        (a reference to the front window) to tell \xc2\xac\n        (a reference to its active tab)\n\n        if not (it exists) then return nil\n        set currentTabTitle to its title\n        set currentTabUrl to its URL\n    end tell\n\nelse if the frontmostBrowser = "Safari" then\n\n    tell application "Safari" to tell \xc2\xac\n        (a reference to the front document)\n\n        if not (it exists) then return nil\n        set currentTabTitle to its name\n        set currentTabUrl to its URL\n    end tell\n\nend if\n\nreturn "[" & currentTabTitle & "](" & currentTabUrl & ")"\n
Run Code Online (Sandbox Code Playgroud)\n\n

不过,我建议实际上将该脚本编写为 shell 脚本。我相信 shell 脚本会比 AppleScript 更快,因为 AppleScript 生成 shell 进程并运行 shell 脚本比 shell 编译和运行 AppleScript 花费更多时间(在本例中\xe2\x80\x94,尽管通常,osascript通常比本机 AppleScript 进程慢)。另一个好处是,通过使用 shell 变量替换,我们可以使生成的脚本更加紧凑​​,将两个浏览器 AppleScript 代码块压缩为一个双用途文本脚本,一旦变量替换完成,该脚本将进行osascript编译made(从而避免了我在评论中提到的运行时/编译时的胡言乱语)。

\n\n

shell (bash) 脚本如下所示:

\n\n
Google Chrome\nScript Editor\nAtom\nMessages\nWhatsApp\nFinder\nSafari\nScript Debugger\nWebTorrent\n
Run Code Online (Sandbox Code Playgroud)\n