Reputation: 652
I'm trying to add right click items with NSMenuItem tools with Finder Inject. I need to change the items according to the path of the clicked file/folder. But it seems that, with finder inject, the menu items are generated before right-click.
In other words, I can't edit the MenuItems regarding on a condition that checks the path of the right-clicked file. Any ideas?
I've tried
-(NSMenuItem *)createMenuItem {
// I need to catch the path of the clicked item here.
NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:@"ITEM TITLE" action:@selector(myMethodClicked:) keyEquivalent:@""];
[menuItem setTarget:self];
return menuItem;
}
I can get path of the clicked file with the method below, see the line:
NSArray *selectedFiles = [[ILFinderMenu sharedInstance] selectedItems];
But I need it before the menuItems are added. Whole function is also added below.
- (void)myMethodClicked:(id)sender {
NSMenuItem *item = (NSMenuItem *)sender;
NSMenu *submenu = [item submenu];
NSArray *selectedFiles = [[ILFinderMenu sharedInstance] selectedItems];
NSString *selectedFilePath = [selectedFiles objectAtIndex:0];
NSString *appPath = @"/eclipse/";
if ([selectedFilePath rangeOfString:appPath].location != NSNotFound) {
// disable backup
NSMenuItem *backupMenuItem = [submenu itemAtIndex:1];
[backupMenuItem setEnabled:NO];
// enable share
NSMenuItem *shareItem = [submenu itemAtIndex:0];
[shareItem setEnabled:YES];
} else {
// disable share
NSMenuItem *backupMenuItem = [submenu itemAtIndex:0];
[backupMenuItem setEnabled:NO];
// enable backup
NSMenuItem *shareItem = [submenu itemAtIndex:1];
[shareItem setEnabled:YES];
}
}
Upvotes: 0
Views: 269
Reputation: 463
Better method to do this is to use finder extensions. If you need more flexibility, you can use notifications to communicate with the main app and change right click menu items dynamically. The official documentation has examples that can help. Here is an app that uses the approach to add custom right click menu items. The page has a link to the source.
Upvotes: 1
Reputation: 22930
You should find another method for MethodSwizzling
. Find method that will get called before menu are generated.
Upvotes: 0