微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐