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

我希望能够从屏幕最前面的窗口中获取所有文本,将其传送到URL解析器并从解析的文本中选择要打开的URL.

为此,我需要一种方法来访问最前面窗口的所有文本内容.我已经接近使用applescript但我无法找到获取实际文本的方法.我怀疑swift可能能够提供帮助,但我很遗憾甚至谷歌这个,因为我的大多数搜索最终只能找到一些简单的方法,比如从Chrome中的标签中获取网址等.

这是我想要的要点:

tell the frontmost application
    get contents of frontmost window as text
end tell

最佳答案 您是否尝试获取特定应用程序的所有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
点赞