objective-cmacoscocoansopenpanel

NSOpenPanel restrict already selected file


I have added NSOpenPanel for selecting folders. But I want achieve following cases:
1. Restrict user to select already selected folders.
2. Restrict user to select sub folders if parent folder already selected.

Or if above cases are not possible then how can check whether parent folder of current selected folder is already selected or not?

//Panel related code
    aPanel = [NSOpenPanel openPanel];
    [aPanel setMessage:@"Choose folders"];
    [aPanel setPrompt:@"Select"];
    [aPanel setNameFieldLabel:@"Location"];
    [aPanel setAllowsMultipleSelection:YES];
    [aPanel setCanChooseDirectories:YES];
    [aPanel setDirectoryURL:[NSURL fileURLWithPath:NSHomeDirectory()]];
    [aPanel setDelegate:self];
    dispatch_async(dispatch_get_main_queue(), ^
    {
        [aPanel beginSheetModalForWindow:self.view.window completionHandler:^(NSInteger result)
        {
            if (result == NSFileHandlingPanelOKButton)
            {
                for (NSURL *folderPath in [aPanel URLs])
                {
                    [files addObject:[folderPath path]];
                }
            }
        }];
    });

//Filter method handle
    - (BOOL)panel:(id)sender shouldEnableURL:(NSURL *)url {
        BOOL result = YES;
        for (NSString *urlPath in files) {
            result  = result && ( [url.path hasPrefix:urlPath] ||  [urlPath isEqualTo:url.path] ? NO : YES);
        }
        return result;
    }

Here files is old selected folder list having string type paths


Solution

  • Have you added NSLog() calls, or used the debugger, to see what is going on? Think again about the conditions under which you need shouldEnableURL to return YES/NO. For example consider:

    By considering the above and using NSLog()/the debugger you should quickly be able to determine the logic required to return the Boolean value you need (whatever it is).

    HTH