Display full folder path in console using electron and Javascript

Viewed 156

Im using electron with a python backend (for a stand alone desktop application) and I need to supply the python script with a directory. With the following code I can get a dialog to open however, it will not output the folder path to the console.

const OpenBtn = document.getElementById('OpenBtn')

OpenBtn.addEventListener('click', function(event) {
  const { dialog } = require('electron').remote;
  //Synchronous
  let dir = dialog.showOpenDialog({properties:["openDirectory"]})
  console.log(dir)
})

I am new to the frontend aspects of creating apps and I am trying to understand what is contained in dir. I see it produces a "promise" (I've tried various ways of accessing the filePaths string, but without success.

There is an HTML button with id=OpenBtn, and I have

        webPreferences: {
            nodeIntegration: true,
            enableRemoteModule: true
        }

in my main.js file.

1 Answers

Either use the synchronous showOpenDialogSync:

let dirs = dialog.showOpenDialogSync({properties:["openDirectory"]})
if (typeof dirs !== "undefined") {
    console.log("Selected paths:");
    console.log(dirs);
}

Or the asynchronous showOpenDialog:

dialog.showOpenDialog({properties: ["openDirectory"]}).then(result => {
    if (result.canceled === false) {
        console.log("Selected paths:");
        console.log(result.filePaths);
    }
}).catch(err => {
    console.log(err);
})
Related