我怎样才能尽可能地从 OSX 上最前面的窗口/文档中获取所有文本?

bru*_*and 5 macos applescript osascript swift

我希望能够从屏幕上最前面的窗口获取所有文本,将其通过管道传输到 url 解析器,并从解析的文本中选择要打开的 url。

为此,我需要一种方法来访问最前面窗口的所有文本内容。我已经通过使用 applescript 接近了,但我找不到获取实际文本的方法。我怀疑 swift 可能会有所帮助,但我不知道如何用谷歌搜索这个,因为我的大多数搜索最终都只找到了一些微不足道的方法,例如从 chrome 中的选项卡获取网址等。

这就是我想要的要点:

tell the frontmost application
    get contents of frontmost window as text
end tell
Run Code Online (Sandbox Code Playgroud)

小智 2

您是否尝试过获取特定应用程序的所有 UI 元素?

set appname to "System Preferences" -------------------------- Set this to the App you want to look at

display dialog "Set the app you want to look at" default answer "System Preferences" buttons {"OK"} default button 1
set appname to text returned of result

set winstuff to "defaultval"
set menustuff to "defaultval"

tell application appname
    activate
end tell

delay 0.5

tell application "System Events"
    tell process appname
        set winstuff to entire contents of front window
        set menustuff to entire contents of menu bar 1
    end tell
end tell
--return winstuff & "\r\r\r\r" & menustuff -- comment this out to get just winstuff
return winstuff -- comment this out too to get just menustuff
--return menustuff
Run Code Online (Sandbox Code Playgroud)