How to get file path from clipboard in ElectronJS

September 26, 2022

β€’

––– views

(Updated on December 19, 2022)

article cover

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 }
7
8 if (process.platform === "win32") {
9 filePath = clipboard
10 .read("FileNameW")
11 .replace(new RegExp(String.fromCharCode(0), "g"), "");
12 }
13
14 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!

Clipboard Manager for Everyone

Β©2022 Xclippy.comAll Rights Reserved