Question

I am trying to get an NSOpenPanel to do the following:

  1. Cannot select files

  2. Can select directories and packages

  3. Cannot see package contents

In order to get the first 2 points I need to use:

[openDlg setCanChooseFiles:NO];
[openDlg setCanChooseDirectories:YES];
[openDlg setTreatsFilePackagesAsDirectories:YES];

However this means that when in column view and a package is selected, the contents of the package are shown. I want the behaviour which occurs when we have [openDlg setCanChooseFiles:YES]; [openDlg setTreatsFilePackagesAsDirectories:NO]; i.e. the package can be selected but the column view browser does not show the contents when it is selected.

Any ideas?

Was it helpful?

Solution 2

I implemented the NSOpenPanelDelegate method panel:shouldEnableURL: as follows:

- (BOOL)panel:(id)sender shouldEnableURL:(NSURL *)url {

    BOOL showObject = NO;

    // This checks if the path is a directory
    [[NSFileManager defaultManager] fileExistsAtPath:[url path] isDirectory:&showObject];

    // This checks if the path is a package
    if ([[NSWorkspace sharedWorkspace] isFilePackageAtPath:[url path]]) {
        showObject = YES;
    }
    return showObject;
}

This doesn't require any further configuration (like setCanChooseDirectories:) and does exactly what I want!

OTHER TIPS

There's a now deprecated method in the NSSavePanel's delegate with a method name of:

- (BOOL) panel: (id) sender shouldShowFilename: (NSString *) filename]

which can be used to tell the save panel to not display certain filenames.

Details about how to use it can be seen in this Apple QA technote, which details how to do exactly the opposite of what you are trying to do (their example is how to choose any file but ignore packages, but you may be able to flip the internal logic around).

Now, remember that I said that the method is "deprecated". The NSSavePanel header file says this:

/* This method is deprecated in 10.6, and will be formally deprecated */
/* in a future release. Use panel:shouldEnableURL:  instead           */
- (BOOL)panel:(id)sender shouldShowFilename:(NSString *)filename;

What NSOpenSavePanelDelegate's panel:shouldEnableURL: apparently does it merely allow or disallow the file from being selectable.

To future-proof your app, you may need to do the respondsToSelector trick to make sure "shouldShowFilename" is still available as an option before using the less desirable "shouldEnableURL" method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top