DEV Community

Cover image for How to get file path from clipboard in ElectronJS
Khoi Doan (Osbkca)
Khoi Doan (Osbkca)

Posted on

1 1

How to get file path from clipboard in ElectronJS

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://', '');
Enter fullscreen mode Exit fullscreen mode

On Windows:

const rawFilePath = clipboard.read('FileNameW');
const filePath = rawFilePath.replace(new RegExp(String.fromCharCode(0), 'g'), '');
Enter fullscreen mode Exit fullscreen mode
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;
};
Enter fullscreen mode Exit fullscreen mode

I used above code for my love app Xclippy

xclippy #osbkca

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay