How to use Callback to get path(s)?


#1

I’m still kind of new to JS and (with me usually doing Node stuff), but I’m having a awful time learning callbacks. So, could someone help me on making a callback with ShowOpenDialog so I can get the path(s)/file(s) that have been selected?


#2

Hey there!

Before I’ll paste any code, you should definitely checkout the Electron API Demos app. Under Native User InterfaceUse system dialogs you have a fully described example.

Regarding your actual question: As stated in the docs you provide the callback as the last parameter in dialog.showOpenDialog([browserWindow, ]options[, callback]). The docs also say that the callback will be called with the selected files as the first given parameter:

If a callback is passed, the API call will be asynchronous and the result will be passed via callback(filenames)

So your callback could look like this:
Note: I left out the other parameters just for clarity, adjust accordingly…

dialog.showOpenDialog(…, …, (filenames) => {
    console.log(filenames); // those are your path files
});

Hope this helps!


#3

Seems like a duplicate.


#4

Thanks @john! This indeed appears to be a duplicate of the topic that @john linked.

We like to unify similar discussions under one topic so that people don’t have to read a bunch of different threads to get the whole story. Please continue the discussion there. I’ll be closing this topic as duplicate.


#5

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.