Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xcode macos-finder中的条件右键单击项目_Xcode_Macos_Finder - Fatal编程技术网

Xcode macos-finder中的条件右键单击项目

Xcode macos-finder中的条件右键单击项目,xcode,macos,finder,Xcode,Macos,Finder,我正在尝试使用带有Finder Inject的NSMenuItem工具添加右键单击项。我需要根据单击的文件/文件夹的路径更改项目。但是,使用finder inject,菜单项似乎是在右键单击之前生成的 换句话说,如果检查右键单击文件的路径,则无法编辑菜单项。有什么想法吗 我试过了 -(NSMenuItem *)createMenuItem {   // I need to catch the path of the clicked item here.         NSMenu

我正在尝试使用带有Finder Inject的NSMenuItem工具添加右键单击项。我需要根据单击的文件/文件夹的路径更改项目。但是,使用finder inject,菜单项似乎是在右键单击之前生成的

换句话说,如果检查右键单击文件的路径,则无法编辑菜单项。有什么想法吗

我试过了

-(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;
}
我可以使用下面的方法获取单击文件的路径,请参见第行:

NSArray *selectedFiles = [[ILFinderMenu sharedInstance] selectedItems];
但是在添加菜单项之前我需要它。下面还添加了整个函数

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

您应该找到另一种方法来使用
MethodSwizzling
。查找在生成菜单之前将被调用的方法。

更好的方法是使用。如果您需要更大的灵活性,可以使用通知与主应用程序通信,并动态更改右键单击菜单项。官方文件中有一些例子可以提供帮助。是一个应用程序,使用该方法添加自定义右键单击菜单项。该页面有一个指向源的链接。

这不会提供问题的答案。若要评论或要求作者澄清,请在他们的帖子下方留下评论。没有人会给你方法名称。OP覆盖了finder插件中的错误方法。