alan
alan

Reputation: 890

Electron showOpenDialog not respecting extensions filter on macOS

I have an Electron app and I'm attempting to open a native file picker dialog and restrict the user to only selecting a single file with some custom extension (call it .foo).

On macOS (Mojave, if that matters), using Electron ^3.0.0, this:

dialog.showOpenDialog({ properties: ['openFile'], filters: [{name: 'Foo
Files', extensions: ['foo']}] })`

Will open a native file picker that only allows the user to select directories.

This:

dialog.showOpenDialog({ properties: ['openFile'], filters: [{extensions: 
 ['foo']}] })

Will open a native file picker that allows me to pick a single file, but allows files with any extension to be selected.

How can I open a native file picker dialog on macOS that restricts the user to pick a single file with a specific extension?

Upvotes: 5

Views: 1196

Answers (1)

shhdharmen
shhdharmen

Reputation: 1453

File filter object won't work without both: name and extensions property. So, the first call from your question should work without any issue:

dialog.showOpenDialog({ properties: ['openFile'], filters: [{name: 'Foo
Files', extensions: ['foo']}] })

Because, to select directories, you need to have openDirectory in properties array. Without that property, it shouldn't allow you to select any directory.

Did you check by selecting any directory? When you try to select any directory (double click it or click on 'Open' button form system dialog), it won't select it, it will open that directory.

Upvotes: 2

Related Questions