8

is there any way that I can pass a URL to a folder on my system which should be the default window which an NSOpenPanel opens? Thanks!

Update:

NSOpenPanel *ads_open = [[NSOpenPanel openPanel] retain];
[ads_open setDirectoryURL:"file://localhost/System/Library/CoreServices/prndrv"];

I am using the above code which is the directory which I would like to be opened by default. However, the default window that I am getting is still the last one that I have accessed and not the one that I have specified. How can I access the URL directory?

Kevin
  • 1,469
  • 2
  • 19
  • 28

4 Answers4

2
NSOpenPanel *ads_open = [NSOpenPanel openPanel];
[ads_open setDirectoryURL:[NSURL URLWithString:@"file://localhost/System/Library/CoreServices/prndrv"]];
Matthieu Riegler
  • 31,918
  • 20
  • 95
  • 134
2

beware to use [NSURL fileURLWithPath:someStringPath] else its not a valid file url:

   NSOpenPanel *panel = [NSOpenPanel openPanel];

// changes promt to Select
[panel setPrompt:@"Select"];

// Enable the selection of files in the dialog.
[panel setCanChooseFiles:NO];

// Enable the selection of directories in the dialog.
[panel setCanChooseDirectories:YES];

//allows multi select
[panel setAllowsMultipleSelection:NO];
if(exists){
    [panel setDirectoryURL:[NSURL fileURLWithPath:lastPath]];
}

[panel beginSheetModalForWindow:self.window
              completionHandler:^(NSInteger returnCode) {
                  if (returnCode == NSOKButton)
                  {
                      .....

              }}];
uti.devel
  • 356
  • 6
  • 17
2

For Swift 3:

let panel = NSOpenPanel()
panel.directoryURL = URL(fileURLWithPath: "smb://servername/path", isDirectory: true)
mbonness
  • 1,612
  • 1
  • 18
  • 20
0

Working example:

NSOpenPanel *panel = [NSOpenPanel openPanel];
[panel setDirectoryURL:[NSURL URLWithString:@"file://localhost/System/Library/CoreServices/"]];

[panel beginSheetModalForWindow:self.window
              completionHandler:^(NSInteger returnCode) {
                  if (returnCode == NSOKButton)
                  {
                      NSURL *theURL = [[panel URLs] objectAtIndex:0];
                  }
              }];
Anne
  • 26,765
  • 9
  • 65
  • 71
  • This is the line that should be doing the trick but for some reason isn't... [ads_open setDirectoryURL:[NSURL URLWithString:@"file://localhost/System/Library/CoreServices/HOBmacgate/prndrv"]]; The file browser is still loading the last URL that was accessed and not the default one specified as a URL. – Kevin Jul 13 '12 at 11:44
  • Are you sure `/localhost/System/Library/CoreServices/HOBmacgate/prndrv` actually exists? Otherwhise `NSOpenPanel` cannot display the directory. – Anne Jul 13 '12 at 12:02