Get path of 'ShowOpenDialog'


#1

When someone selects a folder, what do I do to get the path that they’ve selected? Is it the callback? If so could I get an example? Still quite new to certain things. This is my current code:

function openDia() {
	dialog.showOpenDialog({
		properties: ['openDirectory']

	})
}

How to use Callback to get path(s)?
#2

You provide a callback function:

function openDia() {
	dialog.showOpenDialog({
		properties: ['openDirectory']

	}, selectedFiles => console.log(selectedFiles))
}

#3

Can you explain how you got that callback? I’m awful with callbacks and I don’t know how you got that, as it does work, but I’m wondering how to use that callback outside of it.


#4

It’s in the docs:

dialog.showOpenDialog([browserWindow, ]options[, callback])

The [, callback] part means this function takes an optional callback argument. Then later on it says:

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

The last part callback(filenames) gives you the signature for the callback.

http://electron.atom.io/docs/api/dialog/#dialogshowopendialogbrowserwindow-options-callback


#5

What exactly does a signature for a callback mean? Is this something that is set specifically set by electron?

So in example, if I had

function openDia() {
	dialog.showOpenDialog({
			properties: ['openFile']
		}
	)
}

and I wanted to log the path name in

function test() {

}

how would I use the callback to do this?


#6

The callbacks section of The Art of Node explains how it works.

By adding test as the callback argument to showOpenDialog and adding filenames as the argument to test.