2014-01-22 34 views
0

是否可以從OS X中的其他應用程序訪問視圖元素?例如,有兩個應用程序,一個有NSTextView。從技術上講,第二個應用程序可以從第一個應用程序的文本視圖中讀取文本。訪問另一個可可應用程序的視圖

當您調用GetWindowTextA()的窗口不在您的應用程序中時,Windows中的類似情況。

+0

兩個應用程序在單獨的進程中運行,而不是在同一個應用程序中的兩個窗口,對不對? – Jay

+0

是的,2個不同的應用程序,隱含不同的進程 – Bogdan

+0

在我的回答中增加了一個示例 – markhunte

回答

0

你可以嘗試,並綁定在應用程序1.

而且在應用2 TextView的價值爲NSUserDefaults標準缺省值,讀條目。看看Preferences Utilities Reference如何閱讀另一個應用程序的偏好。

這是一個從Safari獲取下載路徑的快速示例。

#import "AppDelegate.h" 
    CFStringRef theValue; 
    @implementation AppDelegate 

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{ 

    //-- THE PREFERENCE FOR ANOTHER APP 
    CFStringRef appID =CFSTR("com.apple.Safari"); 

     //-- THE PREFERENCE KEY FOR ANOTHER APP:| GETS THE DOWNLOAD FOLDER PATH 
    CFStringRef theKey = CFSTR("DownloadsPath"); 


    Boolean didSynch; 


    //-- TRY AND MAKE SURE THE OTHER APP SYNCHRONISES ITS PREFENCES; 
    //--Writes to permanent storage all pending changes to the preference data for the application, and reads the latest preference data from permanent storage 
    didSynch = CFPreferencesAppSynchronize (
             appID 
             ); 
    if (didSynch) { 
     //-- CALL METHOD TO READ THE PREFERENCE KEY VALUE 
     [self readPrefValue :theKey : appID]; 

     NSLog(@"theValue %@",theValue); 
    } 



    CFRelease(theValue); 
    CFRelease(appID); 
    CFRelease(theKey); 
} 

    - (void)readPrefValue :(CFStringRef) Key : (CFStringRef) appID 
    { 
     theValue = CFPreferencesCopyValue (
              Key, 
              appID, 
              kCFPreferencesCurrentUser, 
              kCFPreferencesAnyHost 
              ); 
    } 



    @end 
+0

非常感謝,我會研究它! – Bogdan

1

這是不可能的(使用公共API)。

您可能有一個使用accessibility控制另一個應用程序的遠程機會,但沒有直接訪問另一個應用程序在另一個進程中的NSTextField

+0

謝謝,我也將研究它,看看它是如何工作的。 – Bogdan

相關問題