How to get file path from clipboard in ElectronJS
September 26, 2022
β’
βββ views(Updated on December 19, 2022)
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:
1const filePath = clipboard.read('public.file-url').replace('file://', '');
On Windows:
1const rawFilePath = clipboard.read('FileNameW');2const filePath = rawFilePath.replace(new RegExp(String.fromCharCode(0), 'g'), '');
You can combine a function to get file path.
1import { clipboard } from "electron";2export const getFilePathFromClipboard = () => {3 let filePath = "";4 if (process.platform === "darwin") {5 filePath = clipboard.read("public.file-url").replace("file://", "");6 }78 if (process.platform === "win32") {9 filePath = clipboard10 .read("FileNameW")11 .replace(new RegExp(String.fromCharCode(0), "g"), "");12 }1314 return filePath;15};
Happy coding! Xclippy
Updates delivered to your inbox!
No spam - unsubscribe at any time!
- subscribers
π£
This article was originally published here.
More articles
If you enjoyed this article, you will find these insightful too!