Open cpgb85 opened 2 years ago
Here's a couple of ways I've discovered it can be done:
main.js
ipcMain.on('select-directory', async (event, arg) => {
const result = await dialog.showOpenDialog(win, {
properties: ['openDirectory']
});
console.log('directories selected', result.filePaths)
event.sender.send('select-directory-response', result.filePaths);
});
renderer.js
I'm using ipcRenderer
and node enabled in my renderer
ipcRenderer.on('select-directory-response', (event, data) => {
console.log(data);
});
But if not, just placing this on the preload.js
should alwo work:
ipcRenderer.on('select-dirs-response', (event, args) => {
alert(args);
});
Following this doc
main.js
Define a handle
that can be invoked by the preload.
ipcMain.handle('select-dirs-handle', async (event, ...args) => {
const result = await dialog.showOpenDialog(mainWindow, {
properties: ['openDirectory']
})
console.log('directories handled', result.filePaths)
return result.filePaths
})
In preload, add another event.data.type for the different name and use async
and await
:
if (evt.data.type === 'select-dirs-handle') {
const result = await ipcRenderer.invoke('select-dirs-handle');
alert(result);
}
Working examples here https://github.com/jaketrent/demo-electron-directory/compare/master...AnEmortalKid:master
@AnEmortalKid Thank you for this fix!
Np, I made a pr since i forgot that part.
What is the method for sending messages back to the web view?