How to pre-select a file in NSOpenPanel -- specifically, a .app bundle?
How to pre-select a file in NSOpenPanel -- specifically, a .app bundle?
- Subject: How to pre-select a file in NSOpenPanel -- specifically, a .app bundle?
- From: Andy Lee <email@hidden>
- Date: Fri, 24 Jun 2016 16:31:27 -0400
How do I present an NSOpenPanel with a particular file pre-selected?
I can pre-select a *directory* by setting openPanel.directoryURL. Since the file I want to select is actually a .app bundle, which is really a directory, you might think I'm in luck, but not quite.
Here's the code I tried:
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
openPanel.treatsFilePackagesAsDirectories = NO;
openPanel.allowsMultipleSelection = NO;
openPanel.canChooseDirectories = NO;
openPanel.canChooseFiles = YES;
openPanel.resolvesAliases = YES;
openPanel.allowedFileTypes = @[@"app"];
openPanel.allowsOtherFileTypes = NO;
NSString *appBundlePath = @"/Applications/TextEdit.app";
openPanel.directoryURL = [NSURL fileURLWithPath:appBundlePath];
[openPanel beginSheetModalForWindow:self.window
completionHandler:^(NSInteger result) {
NSLog(@"Result is %zd", result);
}];
When I run this, the good news is that TextEdit.app gets selected. The bad news is that it is treated as a regular directory, and its "Contents" subdirectory is exposed in the file browser:
<http://imgur.com/g7fmPcK>
If I click away, then click back on TextEdit, I get what I wanted in the first place:
<http://imgur.com/rNQt57O>
I've tried workarounds that Google turned up. One was to use URLWithString: rather than fileURLWithPath:. Another was to go ahead and call a deprecated method like beginSheetForDirectory. These may have worked once upon a time, but they don't for me on 10.11.5.
It almost looks like the ability to pre-select a file was deliberately removed from the NSOpenPanel API. Could that be the case?
--Andy
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden