12
我有一個狀態菜單應用程序,可以使用系統範圍的快捷方式啓動。當應用程序處於活動狀態時,如果我能以某種方式獲取當前正在運行的應用程序中選定的文本,將會非常好。在Cocoa活動應用程序中獲取當前選定的文本
因此,例如我在我的文本編輯器中鍵入內容,選擇文本,打開我的全局快捷方式,我的應用程序出現,現在我想知道文本編輯器中選定的文本。
我至今是以下(採納了How to get global screen coordinates of currently selected text via Accessibility APIs.代碼)
AXUIElementRef systemWideElement = AXUIElementCreateSystemWide();
AXUIElementRef focussedElement = NULL;
AXError error = AXUIElementCopyAttributeValue(systemWideElement, kAXFocusedUIElementAttribute, (CFTypeRef *)&focussedElement);
if (error != kAXErrorSuccess) {
NSLog(@"Could not get focussed element");
} else {
AXValueRef selectedTextValue = NULL;
AXError getSelectedTextError = AXUIElementCopyAttributeValue(focussedElement, kAXSelectedTextAttribute, (CFTypeRef *)&selectedTextValue);
if (getSelectedTextError == kAXErrorSuccess) {
selectedText = (__bridge NSString *)(selectedTextValue);
NSLog(@"%@", selectedText);
} else {
NSLog(@"Could not get selected text");
}
}
if (focussedElement != NULL) CFRelease(focussedElement);
CFRelease(systemWideElement);
這裏的問題是,它不與如Safari和郵件應用工作...
感謝
你解決了這個問題了嗎? –
沒有......但我最終並不需要它......但仍然有興趣知道它將如何在App-Store保存和可靠的方式下工作...... – Georg
同樣在這裏......也想看看一個無Appstore保存方式... –