我正在尝试为使用 JavaScript for Automation 编写的小型应用程序创建停靠菜单。这是我正在使用的代码的精简版本:
ObjC.import("Cocoa");
// Create the menu
var menu = $.NSMenu.alloc.initWithTitle('Menu');
var item = menu.addItemWithTitleActionKeyEquivalent('Item', null, '');
// Register the app delegate
if (typeof $.AppDelegate === 'undefined') {
ObjC.registerSubclass({
name: 'AppDelegate',
superclass: 'NSObject',
protocols: ['NSApplicationDelegate'],
methods: {
"applicationDockMenu:": function (sender) {
$.NSLog('applicationDockMenu');
return menu;
}
}
})
}
// Create the app delegate instance
var appDelegate = $.AppDelegate.alloc.init;
// Assign to the app
$.NSApp.setDelegate(appDelegate);
如果您将其粘贴到 Apple 脚本编辑器中,将其保存为应用程序并运行它,您将在右键单击停靠图标时看到新的菜单项(有时停靠菜单在您激活另一个应用程序之前不会出现。 )
问题是; 单击 Dock 中的应用程序图标或退出应用程序将导致它在控制台中崩溃:
22/03/2016 22:33:59.895 applet[752]: -[AppDelegate handleEvent:withReply:error:]: unrecognized selector sent to instance 0x7fe8e55cf340
看起来有些东西正在尝试调用handleEvent:withReply:error:
,这在 NSApplicationDelegate 协议中没有定义。
任何想法是什么导致了这种行为?