How do you "preselect" a file in the NSOpenPanel dialog
Asked Answered
M

1

6

Does anyone know how to preselect a file in the NSOpenPanel dialog?

This has been asked several times before, but none of those questions were answered with a working solution. Setting nameFieldStringValue has no effect as the open dialog does not have a textfield for the user to type in a name. Setting the directoryURL only sets the current directory in the dialog.

Misdirection answered 19/9, 2013 at 20:2 Comment(0)
I
8

Try like this:-

NSOpenPanel *op=[NSOpenPanel openPanel];
[op setDirectoryURL:[NSURL URLWithString:@"file://localhost/Users/Home/Desktop/yourfile.sh/"]];
[op runModal];
Intinction answered 21/9, 2013 at 11:3 Comment(5)
As I indicated in my question, setting the directoryURL only makes the dialog show that directory. It does not actually select the file.Misdirection
I just ran a test where I hard coded the path and you are correct. It does select the filename. That must mean that in my regular logic I am not properly building the path. Thank-you for the confirmation.Misdirection
It turns out that my problem was using [panel setDirectoryURL: [NSURL URLWithString: path]]; instead of [panel setDirectoryURL: [NSURL fileURLWithPath: path]];Misdirection
Note that this does not work on 10.6, the first OS X version for which this API is available; instead it treats the file if it were an empty directory. (It works fine on 10.7 and 10.8, and I imagine on 10.9 though I haven't tested it.)Billionaire
Interesting that there is an internal difference between URLWithString and fileURLWithPath which produce identical output in NSLog.Tall

© 2022 - 2024 — McMap. All rights reserved.