2013-11-14 22 views
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和郵件應用工作...

感謝

+0

你解決了這個問題了嗎? –

+0

沒有......但我最終並不需要它......但仍然有興趣知道它將如何在App-Store保存和可靠的方式下工作...... – Georg

+0

同樣在這裏......也想看看一個無Appstore保存方式... –

回答

1

這在技術上並不是解決您確切問題的方法,因爲用戶必須從服務菜單中觸發此操作,而不是在觸發菜單欄應用程序時觸發它。您可以使用System Service。您爲您的應用程序創建一項服務,通過一個粘貼板將當前選定的文本發送到菜單欄應用程序。

相關問題