Finder Sync: sender submitted to action is another instance of NSMenuItem

I implemented the Finder Sync extension according to the provided sample provided by Apple.

After clicking on the newly created context menu item, the corresponding sampleAction action is sampleAction . Unfortunately, the sender passed to the method does not match the instance created in menuForMenuKind .

The code

 - (NSMenu *)menuForMenuKind:(FIMenuKind)whichMenu { NSMenuItem* menuItem = [[NSMenuItem alloc] initWithTitle:@"testTitle" action:@selector(sampleAction:) keyEquivalent:@""]; menuItem.tag = 1; // print menu attributes NSLog(@"------------------original menu-----------------"); NSLog(@"menu address: %p", menuItem); NSLog(@"menu tag: %ld", menuItem.tag); NSLog(@"menu title: '%@'", menuItem.title); NSMenu *menu = [[NSMenu alloc] initWithTitle:@""]; [menu addItem:menuItem]; return menu; } - (IBAction)sampleAction:(id)sender { if( [sender isKindOfClass:[NSMenuItem class]]) { NSMenuItem* menuItem = sender; NSLog(@"------------------menu passed to action-----------------"); NSLog(@"menu address: %p", menuItem); NSLog(@"menu tag: %ld", menuItem.tag); NSLog(@"menu title: '%@'", menuItem.title); } } 

displays the following result after I clicked on an item in the context menu

 2014-12-07 19:55:36.923 FinderSync Extension[1265:62630] ------------------original menu---------------- 2014-12-07 19:55:36.923 FinderSync Extension[1265:62630] menu address: 0x6080000abbe0 2014-12-07 19:55:36.924 FinderSync Extension[1265:62630] menu tag: 1 2014-12-07 19:55:36.924 FinderSync Extension[1265:62630] menu title: 'testTitle' 2014-12-07 19:55:40.328 FinderSync Extension[1265:62630] ------------------menu passed to action---------------- 2014-12-07 19:55:40.328 FinderSync Extension[1265:62630] menu address: 0x6080000aba00 2014-12-07 19:55:40.328 FinderSync Extension[1265:62630] menu tag: 0 2014-12-07 19:55:40.328 FinderSync Extension[1265:62630] menu title: '' 

.

Is it possible to associate the sender with the menu item that was clicked?

+7
finder macos nsmenuitem findersync
source share
2 answers

For reference, this is apparently intentional behavior. I filed a bug report with Apple and this was their answer:

This problem behaves as expected based on the following:

Finder Sync does not support specific instances of NSMenuItem *, and very few properties are respected (only name, action, image and enabled). In a future version, the -tag property is likely to be supported, but not an Object representative. - If you need a dynamic set of actions, it should be possible to use functions such as sel_registerName and method_setImplementation (in) to create methods at runtime.

+7
source share

The FIFinderSyncController / FIFinderSyncProtocol offer the best documentation for these menus and methods that I can find.

menu(for menu: FIMenuKind)

The special properties of the menu item are used: name, action, image and included.

Starting from 10.11: tag, state and indentationLevel also work, and submenus are allowed.

The tag property, which was not saved when you asked this question, is now saved on macOS 10.11 +.

The representedObject property is not saved when the callback is received, which would be extremely useful. The NSMenuItem object also has the same meaning.

It is not perfect, but now the saved (and invisible to the user) tag field should be sufficient to transfer information from the creation of the menu and call back the action.

+1
source share

All Articles