epwalsh / obsidian.nvim

Obsidian 🀝 Neovim
Apache License 2.0
4.25k stars 190 forks source link

ObsidianPasteImg <name> and "There is no image data in clipboard" on mac #555

Open RetroTechCorner opened 7 months ago

RetroTechCorner commented 7 months ago

πŸ› Describe the bug

After taking a screenshot on mac (cmd-shift-5) and saving it to the clipboard I try to "paste" to nvim/obsidian note but getting: "There is no image data in clipboard" message. The image is in the clipboard - can paste to other applications.

Config

  {
    "epwalsh/obsidian.nvim",
    ft = "markdown",
    version = "*",
    lazy = true,
    dependencies = {
      -- Required.
      "nvim-lua/plenary.nvim",
    },
    opts = {
      workspaces = {
        {
          name = "personal",
          path = "~/Documents/Notes",
        },
      },
      picker = {
        name = "telescope.nvim",
      },
      notes_subdir = "0_Inbox",
      templates = {
        subdir = "Templates",
        date_format = "%a, %b %e, %Y",
        time_format = "%H:%M",
      },
      -- Specify how to handle attachments.
      attachments = {
        img_folder = "Assets/Images",  -- This is the default
        img_text_func = function(client, path)
          path = client:vault_relative_path(path) or path
          return string.format("![%s](%s)", path.name, path)
        end,
      },
      completion = {
        -- Set to false to disable completion.
        nvim_cmp = true,
        -- Trigger completion at 2 chars.
        min_chars = 2,
      },
      new_notes_location = "0_Inbox",
      note_id_func = function(title)
        local suffix = ""
        if title ~= nil then
          suffix = title:gsub(" ", "-"):gsub("[^A-Za-z0-9-]", ""):lower()
        else
          for _ = 1, 4 do
            suffix = suffix .. string.char(math.random(65, 90))
          end
        end
        return suffix .. "-" .. tostring(os.time())
      end,
    },
  },

Environment

nvim --headless -c 'lua require("obsidian").info()' -c qNVIM v0.9.5
Build type: Release
LuaJIT 2.1.1713517273

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/opt/homebrew/Cellar/neovim/0.9.5/share/nvim"

Run :checkhealth for more info

Obsidian.nvim v3.7.5 (df0c5cce68a02481c0d40d1b25433e368fd12a5a)
Status:
  β€’ buffer directory: nil
  β€’ working directory: /Users/adam/.config
Workspaces:
  βœ“ active workspace: Workspace(name='personal', path='/Users/adam/Documents/Notes', root='/Users/adam/Documents/Notes')
Dependencies:
  βœ“ plenary.nvim: 8aad4396840be7fc42896e3011751b7609ca4119
  βœ“ nvim-cmp: 97dc716fc914c46577a4f254035ebef1aa72558a
Integrations:
  βœ“ picker: TelescopePicker()
  βœ“ completion: enabled (nvim-cmp) βœ— refs, βœ— tags, βœ— new
    all sources:
      β€’ nvim_lsp
      β€’ luasnip
      β€’ buffer
      β€’ nvim_lua
      β€’ path
Tools:
  βœ“ rg: ripgrep 14.1.0
Environment:
  β€’ operating system: Darwin
Config:
  β€’ notes_subdir: 0_Inboxn
epwalsh commented 6 months ago

Is the image a PNG? I believe at the moment only PNG format is supported.

RetroTechCorner commented 6 months ago

It seems it also stores some metadata in the few first bytes and it seems it's this for the screenshot: 0xC2 0xAB 0x63 0x6C 0x61 0x73 0x73 0x20 0x50 0x4E 0x47 0x66 0xC2 0xBB 0x3A 0xC2 0xAB 0x64 0x61 0x74 0x61 0x20

The rest is a png (or PNGf to be precise). Here's how pillow handles that:

def grabclipboard():
    if sys.platform == "darwin":
        fh, filepath = tempfile.mkstemp(".png")
        os.close(fh)
        commands = [
            'set theFile to (open for access POSIX file "'
            + filepath
            + '" with write permission)',
            "try",
            "    write (the clipboard as Β«class PNGfΒ») to theFile",
            "end try",
            "close access theFile",
        ]
        script = ["osascript"]
        for command in commands:
            script += ["-e", command]
        subprocess.call(script)

        im = None
        if os.stat(filepath).st_size != 0:
            im = Image.open(filepath)
            im.load()
        os.unlink(filepath)
        return im

But I think stripping first few bytes would work as well (rather that calling osascript) pypaperclip also seems to be able to do it https://github.com/asweigart/pyperclip

epwalsh commented 6 months ago

I'm happy to review a PR for making this functionality more robust

RetroTechCorner commented 6 months ago

I'm certainly not skilled enough to create the PR but hopefully somebody will pick it up :)

ibash commented 5 months ago

@RetroTechCorner did you brew install pngpaste? I was getting the same error until I installed that.

Mo0rBy commented 4 months ago

Not sure if I should be creating a new issue for this or not as it is related.

I can copy (using cmd + c) a PNG image while inside Finder and use the ObsidianPasteImg to paste it into a README or other .md file, but instead of seeing the actual image, I just see the PNG file icon, rather than the actual image itself:

Screenshot 2024-07-22 at 11 04 13

Screenshot 2024-07-22 at 11 04 20

If you want me to create a new issue for this, as it is a different but related issue, let me know and I'd be happy to do so.

willisplummer commented 3 months ago

I'm running into the original issue. I get "There is no image data in the clipboard".

I've confirmed that I've copied a png, and I've run brew install pngpaste and confirmed it's in my Path (I can run pngpaste on the command line and get the usage suggestions back)

Let me know if you have any suggestions or if there's any other info I can provide to help debug this!

mccuna commented 2 months ago

I'm running into the original issue. I get "There is no image data in the clipboard".

I've confirmed that I've copied a png, and I've run brew install pngpaste and confirmed it's in my Path (I can run pngpaste on the command line and get the usage suggestions back)

Let me know if you have any suggestions or if there's any other info I can provide to help debug this!

What version are you using? Is it the latest commit or the latest release? I had the same issue while using the latest release. Updating to the latest commit by removing the version = * entry from the plugin config solved this issue for me.

mccuna commented 2 months ago

Not sure if I should be creating a new issue for this or not as it is related.

I can copy (using cmd + c) a PNG image while inside Finder and use the ObsidianPasteImg to paste it into a README or other .md file, but instead of seeing the actual image, I just see the PNG file icon, rather than the actual image itself:

Screenshot 2024-07-22 at 11 04 13 Screenshot 2024-07-22 at 11 04 20

If you want me to create a new issue for this, as it is a different but related issue, let me know and I'd be happy to do so.

@Mo0rBy have you managed to solve this issue? I'm running into this issue too and I'm struggling to find a fix for it. I believe this might be related to pngpaste itself because it reproduces not only when running ObsidianPasteImg, but when running pngpaste dummy.png in the terminal directly as well.

mccuna commented 2 months ago

Not sure if I should be creating a new issue for this or not as it is related. I can copy (using cmd + c) a PNG image while inside Finder and use the ObsidianPasteImg to paste it into a README or other .md file, but instead of seeing the actual image, I just see the PNG file icon, rather than the actual image itself: Screenshot 2024-07-22 at 11 04 13 Screenshot 2024-07-22 at 11 04 20 If you want me to create a new issue for this, as it is a different but related issue, let me know and I'd be happy to do so.

@Mo0rBy have you managed to solve this issue? I'm running into this issue too and I'm struggling to find a fix for it. I believe this might be related to pngpaste itself because it reproduces not only when running ObsidianPasteImg, but when running pngpaste dummy.png in the terminal directly as well.

I've managed to get it working. My solution was that instead of pressing cmd+c in Finder to open the image in Preview and then press cmd+c in Preview.

willisplummer commented 2 months ago

@mccuna thanks for the followup! I hadn't tried to paste an image since commenting, but it's now working for me (my lazy lock file is pointed at commit ae1f76a75c7ce36866e1d9342a8f6f5b9c2caf9b)