I'm trying to create a dock menu for a small application written using JavaScript for Automation. Here's a stripped down version of the code I'm using:
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);
If you paste this into the Apple Script Editor, save it as an Application and run it, you'll see the new menu item when you right click the dock icon (sometimes the dock menu won't appear until you make another app active.)
The problem is; clicking the app Icon in the dock or quitting the app will cause it to crash with this in the Console:
22/03/2016 22:33:59.895 applet[752]: -[AppDelegate handleEvent:withReply:error:]: unrecognized selector sent to instance 0x7fe8e55cf340
It looks like something is trying to call handleEvent:withReply:error:
, which isn't defined in the NSApplicationDelegate protocol.
Any ideas what's causing this behaviour?