To get file path of a file which you have copied on the your computer through ElectronJS depends on the operating system run the Electron application on.
In this article, I work on two popular operating system MacOs and Windows
On MacOS:
const filePath = clipboard.read('public.file-url').replace('file://', '');
On Windows:
const rawFilePath = clipboard.read('FileNameW'); const filePath = rawFilePath.replace(new RegExp(String.fromCharCode(0), 'g'), '');
import { clipboard } from "electron"; export const getFilePathFromClipboard = () => { let filePath = ""; if (process.platform === "darwin") { filePath = clipboard.read("public.file-url").replace("file://", ""); } if (process.platform === "win32") { filePath = clipboard .read("FileNameW") .replace(new RegExp(String.fromCharCode(0), "g"), ""); } return filePath; };
I used above code for my love app Xclippy
Top comments (0)