下面是一个可打印突出显示的文本的代码段,但是,它不适用于所有应用程序。例如,我知道一个不兼容的应用程序是Mail。
我应该搜索哪些属性以在Mail之类的应用中获取突出显示的文本:)
func getHighlightedText() -> AnyObject? {
let systemWideElement = AXUIElementCreateSystemWide()
var focusedElement: AnyObject?
let focusedCode = AXUIElementCopyAttributeValue(systemWideElement, "AXFocusedUIElement" as CFString, &focusedElement)
if (focusedCode == AXError.success) {
var selectedText: AnyObject?
let textCode = AXUIElementCopyAttributeValue(focusedElement as! AXUIElement, "AXSelectedText" as CFString, &selectedText)
if (textCode == AXError.success) {
return selectedText
}
}
return nil
}
sleep(3) #enough time to switch to another app and highlight the text
print(getHighlightedText())