I'm currently working on a react/electron
app and I want to be able to copy a file that's outside the app (could be any file type) using ctrl+c
or right click copy.
How can I retrieve that file's name and path inside my app? I've tried navigator.clipboard.readText()
and .read()
and haven't had any luck.
Unfortunately in Electron, clipboard is still highly platform-dependant requiring different code depending on which platform you're running. Here's a snippet for a single file to get you started. If you need access to multiple files, see this snippet.
const { clipboard } = require('electron')
let text = null
if(process.platform === 'darwin') { // MacOS
text = clipboard.read('public.file-url')
} else { // Windows
text = clipboard.readBuffer('FileNameW').toString('ucs2')
} // TODO: Linux
console.log(text);
Depending on your presentation, you may need to convert to a human readable format (e.g. file:///
vs. C:\
, etc)