Is there any way to just grab the image from the clipboard and paste it into Emacs?
Currently, if I wanted to grab an image from the clipboard into Emacs, what I did would be:
- Take the screenshot using my favorite screenshot tool.
- Save the image into my disk.
- Open file manager, and drag the image into Emacs, that is when
org-downloadcomes in and saves the image in Emacs.
The process is inconvenient, what I want is:
- Take the screenshot using my favorite screenshot tool.
- Press Cmd/Ctrl + c to copy the image into the clipboard.
- 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?
Same question...
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/
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.
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.
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.
@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")
Thanks. I later found this solution which also works nice.
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.
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-clipboardand 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 usingC-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.
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))))
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.
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.
FWIW https://www.gnu.org/software/emacs/manual/html_node/elisp/Yanking-Media.html yank-media-type SHOULD be the way to go
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-screenshotor 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))
@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. 👍