How to get file path in clipboard?

l am developing a plugin to auto upload image just like in typora.

Now when l copy image file in my system(win), l can not get the file path.(l use the ClipboardEvent api.)

how can l use the electron clipboard api?

1 Like

Please let me know if you find an answer to this.

1 Like

I was just looking at the same thing. The electron API is there, so it works to do:

const electron = require('electron')
const clipboard = electron.clipboard;

and then make use of clipboard features, e.g. clipboard.writeText(result)

I’m too hazy on javascript imports to understand why I can’t do e.g. import {clipboard} from 'electron', or how to bring it in in a type safe way, but this does at least get text into the clipboard.

Another follow up - it’s best not to use the Electron API, see obsidian-releases/plugin-review.md at master · obsidianmd/obsidian-releases · GitHub

But you can just use navigator.clipboard.readText() and navigator.clipboard.writeText() to access the clipboard

1 Like

You can use this method to get file path.

import { clipboard } from "electron";

  isCopyImageFile() {
    let filePath = "";
    const os = this.getOS();

    if (os === "Windows") {
      var rawFilePath = clipboard.read("FileNameW");
      filePath = rawFilePath.replace(
        new RegExp(String.fromCharCode(0), "g"),
        ""
      );
    } else if (os === "MacOS") {
      filePath = clipboard.read("public.file-url").replace("file://", "");
    } else {
      filePath = "";
    }
    return this.isAssetTypeAnImage(filePath);
  }
1 Like

Thanks! Do you happen to know how to get filePath when OS is Linux? :slightly_smiling_face: Anyway this looks good, I need to try it some time.