Cannot send NSDistributedNotification's from SIMBL plugin
I am developing an SIMBL plugin for TextEdit, the plugin adds an additional menu. The menu has a number of items that when selected (among other things) send messages to my own application. To do this I have been using NSDistributedNotificationCenter however I am unable开发者_如何学Go to send notifications from my bundle within TextEdit.
I have checked the code I am using in a separate application and it seems to work so I do not understand why my bundle in TextEdit is not able to send messages.
The post notification code:
NSDictionary *user = [NSDictionary dictionaryWithObjectsAndKeys:
@"TextEdit", @"applicationName",
@"289", @"applicationVersion",
@"1", @"menuItem", nil];
NSString *observedObject = @"com.drake.DDX";
NSDistributedNotificationCenter *center = [NSDistributedNotificationCenter defaultCenter];
[center postNotificationName:@"DDXNotification" object:observedObject userInfo:user deliverImmediately:YES];
And the code in the receiver:
NSString *observedObject = @"com.drake.DDX";
NSDistributedNotificationCenter *center = [NSDistributedNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(receiveNotification:) name:@"DDXNotification" object:observedObject];
This code seems to work if I add it to another one of my applications so it makes me think something is going on in TextEdit or SIMBL that I am not aware of.
I have checked that the menu items are executing the method properly by showing an NSAlert in TextEdit as soon as the method is executed.
Any help is appreciated.
(Edit) I have found out that when I post a notification if the userInfo dictionary is nil the notification is received by my application so it appears there is a problem with the NSDictionary I am sending or the receiver method signature.
I don't know why your NSDistributedNotificationCenter isn't working. However I think I can give you a different way to skin this cat.
Design a URL scheme along the lines of myapp://menu/menu_item?arguments.
The important bit is the myapp scheme.
Follow the information here to register your app as the default handler of this scheme.
Now in response to a menu selection compose an appropriate URL using myapp as the scheme and call
NSURL* url = [NSURL URLWithString:@"myapp://menu/menu_item"];
[[NSWorkspace sharedWorkspace]openURL:url];
精彩评论