无法从 SIMBL 插件发送 NSDistributedNotification

发布于 2024-12-02 10:51:40 字数 1288 浏览 6 评论 0原文

我正在为 TextEdit 开发一个 SIMBL 插件,该插件添加了一个附加菜单。该菜单有许多项目,当选择这些项目时(除其他外)会将消息发送到我自己的应用程序。为此,我一直在使用 NSDistributedNotificationCenter,但是我无法从 TextEdit 中的捆绑包发送通知。

我已经检查了我在单独的应用程序中使用的代码,它似乎可以工作,所以我不明白为什么我在 TextEdit 中的包无法发送消息。

发布通知代码:

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];

以及接收器中的代码:

NSString *observedObject = @"com.drake.DDX";
    NSDistributedNotificationCenter *center = [NSDistributedNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(receiveNotification:) name:@"DDXNotification" object:observedObject];

如果我将其添加到我的另一个应用程序中,此代码似乎可以工作,因此它让我认为 TextEdit 或 SIMBL 中发生了一些我不知道的事情。

我已经通过在执行方法后立即在 TextEdit 中显示 NSAlert 来检查菜单项是否正确执行该方法。

任何帮助表示赞赏。

(编辑)我发现,当我发布通知时,如果 userInfo 字典为零,则我的应用程序会收到通知,因此我发送的 NSDictionary 或接收器方法签名似乎存在问题。

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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

楠木可依 2024-12-09 10:51:40

我不知道为什么你的 NSDistributedNotificationCenter 不起作用。不过我想我可以给你一种不同的方法来给这只猫剥皮。

按照 myapp://menu/menu_item?arguments 的方式设计 URL 方案。

重要的一点是 myapp 方案。

按照此处的信息将您的应用注册为默认处理程序该计划的。

现在,响应菜单选择,使用 myapp 作为方案编写适当的 URL 并调用

NSURL* url = [NSURL URLWithString:@"myapp://menu/menu_item"];
[[NSWorkspace sharedWorkspace]openURL:url];

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文