Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make NSOpenPanel open a custom directory

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?

like image 828
Kevin Avatar asked Jul 13 '12 10:07

Kevin


3 Answers

NSOpenPanel *ads_open = [NSOpenPanel openPanel];
[ads_open setDirectoryURL:[NSURL URLWithString:@"file://localhost/System/Library/CoreServices/prndrv"]];
like image 155
Matthieu Riegler Avatar answered Nov 11 '22 12:11

Matthieu Riegler


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)
                  {
                      .....

              }}];
like image 32
uti.devel Avatar answered Nov 11 '22 12:11

uti.devel


For Swift 3:

let panel = NSOpenPanel()
panel.directoryURL = URL(fileURLWithPath: "smb://servername/path", isDirectory: true)
like image 2
mbonness Avatar answered Nov 11 '22 12:11

mbonness