Question

I have a working App that sends an NSNotification to itself when the User pushes a button in the window (Xcode using PyObjC):

from Foundation import *
from AppKit import *
import objc

class SpeakAppDelegate(NSObject):
    def applicationDidFinishLaunching_(self, sender):
        NSLog("Application really did finish launching.")
        nc = NSNotificationCenter.defaultCenter()
        nc.addObserver_selector_name_object_(
            self, "mycallback:", 'love_note', None)
            #self, "mycallback:", None, None)

    @objc.signature('v@0:@8')
    def mycallback_(self,note):
        print 'note'
        print note.description()

    @objc.IBAction
    def button_(self,sender):
        print sender, 'button'
        nc = NSNotificationCenter.defaultCenter()
        nc.postNotificationName_object_userInfo_(
            'love_note', None, {'path':'xyz'})

(A detail: the signature is probably not exactly right, but it works).

Leave it running. Now I want to figure out how to send the same notification to this App from another application, for example:

// gcc tell.m -o test -framework Foundation
#import <Foundation/Foundation.h>

int main() {
    NSNotificationCenter *nc;
    nc = [NSNotificationCenter defaultCenter];
    [nc postNotificationName:@"love_note"
                      object:nil
                    userInfo:nil ];
    return 0;
}

I notice that if I un-comment the line in the first App then I get lots of other notifications, but they are all from events related to my App. I never hear anything from outside. How can I send a notification between processes? And then, is there any way to send a notification from the command line? Thanks.

Update: Simply substitute NSDistributedNotificationCenter above, and the example works.

Was it helpful?

Solution

I don't believe there is a way to do communication between two apps using NSNotificationCenter. I have not used it before, but I believe two apps communicating is more of a job for Distributed Objects.

From Apple's documentation:

Each process has a default notification center that you access with the NSNotificationCenter +defaultCenter class method. This notification center handles notifications within a single process. For communication between processes on the same machine, use a distributed notification center (see “NSDistributedNotificationCenter”).

EDIT:

It does seem like NSDistributedNotificationCenter could also do what you are looking for without getting deep into Distributed Objects.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top