Make NSOpenPanel open a custom directory
Asked Answered
T

4

9

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?

Truett answered 13/7, 2012 at 10:19 Comment(3)
How could this even work. setDirectoryURL: takes an NSURL not a Char* ...Denitadenitrate
So what should I add for the path to be seen as an NSURL?Truett
[NSURL URLWithString:@"file://your/path/here"]Denitadenitrate
D
2
NSOpenPanel *ads_open = [NSOpenPanel openPanel];
[ads_open setDirectoryURL:[NSURL URLWithString:@"file://localhost/System/Library/CoreServices/prndrv"]];
Denitadenitrate answered 13/7, 2012 at 11:24 Comment(0)
Y
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)
                  {
                      .....

              }}];
Yeah answered 16/6, 2013 at 14:0 Comment(0)
P
2

For Swift 3:

let panel = NSOpenPanel()
panel.directoryURL = URL(fileURLWithPath: "smb://servername/path", isDirectory: true)
Pentagrid answered 2/11, 2016 at 19:17 Comment(0)
A
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];
                  }
              }];
Araldo answered 13/7, 2012 at 11:23 Comment(2)
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.Truett
Are you sure /localhost/System/Library/CoreServices/HOBmacgate/prndrv actually exists? Otherwhise NSOpenPanel cannot display the directory.Araldo

© 2022 - 2024 — McMap. All rights reserved.