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

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay