Bogdan
Bogdan

Reputation: 863

Access a view from another Cocoa application

Is it possible to access a view element from another application in OS X? For example there are two apps and one has an NSTextView. It is technically possible that the second app can read the text from the first app's text view?

A similar case in Windows when you call GetWindowTextA() for a window which is not in your application.

Upvotes: 0

Views: 237

Answers (2)

markhunte
markhunte

Reputation: 6932

You could try and bind the textView's value to the NSUserDefaults standard defaults in app 1.

And in app 2, read the entry. Have a look at Preferences Utilities Reference in how to read another apps preference.

Here is a quick example that gets the download path from 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

Upvotes: 0

Jay
Jay

Reputation: 6638

It's not possible (with public APIs).

You might have a remote chance controlling another app using accessibility, but no such thing as directly accessing the NSTextField of another app in another process.

Upvotes: 1

Related Questions