org-download icon indicating copy to clipboard operation
org-download copied to clipboard

Is there any way to just grab the image from the clipboard and paste it into Emacs?

Open searene opened this issue 5 years ago • 15 comments

Currently, if I wanted to grab an image from the clipboard into Emacs, what I did would be:

  1. Take the screenshot using my favorite screenshot tool.
  2. Save the image into my disk.
  3. Open file manager, and drag the image into Emacs, that is when org-download comes in and saves the image in Emacs.

The process is inconvenient, what I want is:

  1. Take the screenshot using my favorite screenshot tool.
  2. Press Cmd/Ctrl + c to copy the image into the clipboard.
  3. Go back to Emacs and Press Cmd/Ctrl + v to paste the image from the clipboard into Emacs.

Is it possible to be done with org-download?

searene avatar Jul 15 '20 01:07 searene

Same question...

lroolle avatar Jul 24 '20 16:07 lroolle

I figured out how to do this using org-download by customizing org-download-screenshot-method to a command that writes the clipboard to a file on disk. On macOS https://github.com/jcsalterego/pngpaste can be used, on other systems there must be similar utilities but I don't know them. My config is like this:

  (use-package org-download
    :after org
    :defer nil
    :custom
    (org-download-method 'directory)
    (org-download-image-dir "images")
    (org-download-heading-lvl nil)
    (org-download-timestamp "%Y%m%d-%H%M%S_")
    (org-image-actual-width 300)
    (org-download-screenshot-method "/usr/local/bin/pngpaste %s")
    :bind
    ("C-M-y" . org-download-screenshot file)
    :config
    (require 'org-download))

I wrote a bit more details here: https://zzamboni.org/post/how-to-insert-screenshots-in-org-documents-on-macos/

zzamboni avatar Aug 11 '20 22:08 zzamboni

There's also an option "xclip" for org-download-screenshot-method that grabs the image from the clipboard. See M-x customize-group org-download.

abo-abo avatar Aug 13 '20 08:08 abo-abo

As of version (org-download-20200914.1558), you simply put point (cursor) where you want to paste an image from the clipboard. You then use M-x org-download-clipboard and voila, an image is saved to a directory (which you customized) and the link is inserted in your buffer at point. You can view the images inline using C-c C-x C-v. This works even on Windows 10 provided you have installed a version of Emacs compiled with the required DLLs.

pabloaperezfernandez avatar Oct 01 '20 16:10 pabloaperezfernandez

Have you found an org-download-screenshot-method which works from Windows 10 into emacs running under WSL2? The download file gets created in the current directory but is empty. No error is given. The above set-up works perfect under linux mint - with no org-download-screenshot-method given.

torbjornp avatar Oct 19 '20 12:10 torbjornp

@torbjonp: I installed the native Windowsversion of Imagemagick on q:\ImageMagick (mounted at /q: inside wsl2) and defined org-download-screenshot-method as: (setq org-download-screenshot-method "/q:/ImageMagick/convert.exe clipboard:myimage %s")

tokka8 avatar Sep 16 '21 10:09 tokka8

Thanks. I later found this solution which also works nice.

torbjornp avatar Sep 17 '21 06:09 torbjornp

I have the following snipped in my org config for this

  ;; inspired by https://zzamboni.org/post/how-to-insert-screenshots-in-org-documents-on-macos/

(use-package org-download
  :after org
  :defer nil
  :custom
  (org-download-method 'directory)
  (org-download-image-dir "~/journal/_resources")
  (org-download-heading-lvl 0)
  (org-download-timestamp "org_%Y%m%d-%H%M%S_")
  (org-image-actual-width 900)
  (org-download-screenshot-method "xclip -selection clipboard -t image/png -o > '%s'")
  :bind
  ("C-M-y" . org-download-screenshot)
  :config
  (require 'org-download))

Using the simple but awesome screenshot tool flameshot I initiate a screenshot, copy it to clipboard using Ctrl+C (in flameshot) and then paste it to my org file using C-M-y using org-download.

stefan2904 avatar Jan 14 '22 14:01 stefan2904

As of version (org-download-20200914.1558), you simply put point (cursor) where you want to paste an image from the clipboard. You then use M-x org-download-clipboard and voila, an image is saved to a directory (which you customized) and the link is inserted in your buffer at point. You can view the images inline using C-c C-x C-v. This works even on Windows 10 provided you have installed a version of Emacs compiled with the required DLLs.

Thank you so much! Except for your suggestion, any other suggestions won't work.

ChloeZhou1997 avatar Nov 02 '22 03:11 ChloeZhou1997

Of course we can conditionally set org-download-screenshot-method, which is useful for people who use multiple operating systems:

(use-package org-download
  :ensure t
  :after org
  :config
  (setq-default
   org-download-image-dir "assets"
   ;; Basename setting seems to be simply ignored.
   org-download-screenshot-basename ".org.png"
   org-download-timestamp "org_%Y%m%d-%H%M%S_"
   org-download-heading-lvl nil)
  :custom
  (org-download-screenshot-method
   (cond
    ((eq system-type 'gnu/linux)
     "xclip -selection clipboard -t image/png -o > '%s'")
    ((eq system-type 'darwin)
     "pngpaste %s")))
  :bind
  (:map org-mode-map
        (("C-M-y" . org-download-screenshot)
         ("s-y" . org-download-yank))))

FernandoBasso avatar Apr 19 '23 21:04 FernandoBasso

For emacs 28.2 installed in an MSYS2 environment (with powershell 5.1), the following is working well:

(defun my-save-image-from-clipboard (dest-file-path)
  "This saves any image that might be in the windows clipboard to the file at DEST-FILE-PATH"
  (let* ((win-fname (replace-regexp-in-string "\n\\'" "" (shell-command-to-string (concat "cygpath -w " dest-file-path))))
         (cmd-str (concat "powershell.exe -Command \"(Get-Clipboard -Format image).Save('" win-fname "')\"")))
    (shell-command cmd-str)))

and then

(setq org-download-screenshot-method #'my-save-image-from-clipboard)

Finally just use M-x org-download-screenshot or map it to the keybinding of your choice.

edgimar avatar May 16 '23 20:05 edgimar

As far as I know Emacs itself can access the Clipboard and thus should be able to save the content to disk without requuring any external tool?

Still looking fpr a fool-proof solution to paste images into org or markdown documents.

the42 avatar Oct 13 '23 08:10 the42

FWIW https://www.gnu.org/software/emacs/manual/html_node/elisp/Yanking-Media.html yank-media-type SHOULD be the way to go

the42 avatar Feb 22 '24 15:02 the42

For emacs 28.2 installed in an MSYS2 environment (with powershell 5.1), the following is working well:

(defun my-save-image-from-clipboard (dest-file-path)
  "This saves any image that might be in the windows clipboard to the file at DEST-FILE-PATH"
  (let* ((win-fname (replace-regexp-in-string "\n\\'" "" (shell-command-to-string (concat "cygpath -w " dest-file-path))))
         (cmd-str (concat "powershell.exe -Command \"(Get-Clipboard -Format image).Save('" win-fname "')\"")))
    (shell-command cmd-str)))

and then

(setq org-download-screenshot-method #'my-save-image-from-clipboard)

Finally just use M-x org-download-screenshot or map it to the keybinding of your choice.

This config is working quite perfectly on win10. I need to store the screenshot to the folder named the org headline, this might make the file hierarchy more concise:

(use-package! org-download
  :config
  (defun jie/org-download-screenshot-advice ()
    "Customize the 'org-download-screeshot. "
    (setq org-download-method 'directory
          org-download-image-dir (concat (file-name-sans-extension (buffer-file-name)) "-img")
          org-download-link-format "[[file:%s]]\n"
          org-download-abbreviate-filename-function #'file-relative-name
          org-download-link-format-function #'org-download-link-format-function-default))
  (advice-add 'org-download-screenshot :before #'jie/org-download-screenshot-advice))

LloydLore avatar Mar 05 '24 03:03 LloydLore

@torbjonp: I installed the native Windowsversion of Imagemagick on q:\ImageMagick (mounted at /q: inside wsl2) and defined org-download-screenshot-method as: (setq org-download-screenshot-method "/q:/ImageMagick/convert.exe clipboard:myimage %s")

@tokka8: Thank you! I have been researching this for some time now - this is by far the easiest solution and it just works without interfering with the native org-download workflow. 👍

TheBoert avatar Jul 30 '24 07:07 TheBoert