win10的Emacs中,能否在org-mode中插入剪贴板中的截图?

例如,使用win10自带的截图工具截图后,会自动保存到剪贴板上,希望能很很简单的粘贴到org-mode中,能实现吗?

1 个赞

里面有个 org-download-clipboard

1 个赞

我看了眼源代码,不能用系统剪贴板,需要第三方工具 ImageMagick 如果不觉得我写的乱,其实推荐用一下这个

(defun org-screenshot-on-windows10 ()
  (interactive)
  (setq full-file-name (file-name-sans-extension (file-name-nondirectory buffer-file-name)))
  ;; 如果文件名的长度小于14,放到mainImage文件夹下面
  (if (< (length full-file-name) 14)
      (setq before-file-name-part "main")
    ;;否则,判断文件中是否含有中文(专门给org roam做的优化,不通用,但是也不想改了)
    (if (string-match "\\cc" full-file-name)
        (setq before-file-name-part  (substring (file-name-sans-extension (file-name-nondirectory buffer-file-name)) 0 14))
      (setq before-file-name-part (substring (file-name-sans-extension (file-name-nondirectory buffer-file-name)) 15))))

  (setq imagefile (concat "./" before-file-name-part "Image/"))
  (unless (file-exists-p imagefile)
    (make-directory imagefile))
  (setq filename (concat (make-temp-name (concat imagefile
                                                 "_"
                                                 (format-time-string "%Y%m%d_%H%M%S_")))
                         ".png"))
  (shell-command (concat "powershell -command \"Add-Type -AssemblyName System.Windows.Forms;if ($([System.Windows.Forms.Clipboard]::ContainsImage())) {$image = [System.Windows.Forms.Clipboard]::GetImage();[System.Drawing.Bitmap]$image.Save('"
                         filename "',[System.Drawing.Imaging.ImageFormat]::Png); Write-Output 'clipboard content saved as file'} else {Write-Output 'clipboard does not contain image data'}\""))
  (insert (concat "[[file:" filename "]]"))
  (org-display-inline-images))
8 个赞

好强的powershell功力,我也献丑一个抄来拼拼凑凑糊出来的版本:

(defun solarion/org-download-paste-clipboard-wsl ()
  "to simplify the logic, use c:/Users/Public as temporary directory, and move it into current directory"
  (interactive)
  (let* ((powershell (executable-find "powershell.exe"))
         (file-base-name (format-time-string "image-%Y%m%d_%H%M%S.png"))
         (file-name (read-string (format "Filename [%s]: " file-base-name) nil nil file-base-name))
         (file-path-wsl (concat "./image/" file-name)))
    (shell-command (concat powershell " -command \"(Get-Clipboard -Format Image).Save(\\\"C:/Users/Public/" file-name "\\\")\""))
    (make-directory "./image" t)
    (rename-file (concat "/mnt/c/Users/Public/" file-name) file-path-wsl)
    (insert (concat "#+ATTR_LATEX: :width \\linewidth\n"))
    (org-indent-line)
    (insert (concat "[[file:" file-path-wsl "]]"))
    (org-display-inline-images)))

最简单的方式 用chocolatey下载imagemagick

cinst -y imagemagick

然后把设置一个org-image-paste函数包装magick命令

magick clipboard: “x.png”

1 个赞
(use-package org-download
  :bind
  (:map
   org-mode-map
   ("<f9>" . org-download-screenshot)
   ("<f10>" . +org-download-clipboard))
  :init
  (defun +org-download-clipboard (&optional basename)
    (interactive)
    (if IS-WINDOWS
        ;; NOTE: scoop install graphicsmagick
        (let ((org-download-screenshot-method "gm convert clipboard: %s"))
          (org-download-screenshot basename))
      (org-download-clipboard basename)))
  :config
  (if IS-WINDOWS
      ;; NOTE: scoop install irfanview
      (setq org-download-screenshot-method "irfanview /capture=4 /convert=\"%s\"")
    (setq org-download-screenshot-method "scrot -s %s")))

请问一下,这个适用于wsl吗?

IS-WINDOWS是什么?

(defconst is-windows (memq system-type 'windows))

之类,手机打字麻烦,将就看看哈

(defconst IS-WINDOWS (eq system-type 'windows-nt))

我也试图使用irfanview来截图,命令是i_view64 /capture=4 /convert=%s。使用M-x org-download-scrennshot命令后,只能当前窗口截图,没有办法切换窗口,您没遇到这个问题吗?怎么解决的?

我很久没在 Emacs 中粘贴图片了。

以前在 Windows 10 中用过 org-download + irfanview 进行粘贴,后来觉得 Emacs 应该专心处理文本就好,于是干脆不用图片了。

好巧,今天也在研究这个,不过我是从 Windows 粘贴到 WSL 里。 这里有一个 workaround:How to work with WSL? · Issue #178 · abo-abo/org-download · GitHub

直接复制粘贴,一次成功。膜拜大佬! 我使用flameshot、imagemagick、irfanview等搞了一天都没搞定。 不需要irfanview,不需要wsl,真赞!

反复看了几遍代码,受益颇多,感谢大佬!

我复制粘贴没有用,能详细说一下怎么使用吗?

把代码复制粘贴到.emacs

M-x eval-buffle

C-x C-f exam.org

Win+Shift+S进行截图

M-x org-screenshot-on-windows10

应该可以看到截图了,如果没有,按键C-c C-x C-v两遍

可以了,谢谢

感谢大佬的分享,我用在我的配置里,提示: syntax error near unexpected token (' /usr/bin/bash: command substitution: line 2: [System.Windows.Forms.Clipboard]::ContainsImage())) {$image = [System.Windows.Forms.Clipboard]::GetImage();[System.Drawing.Bitmap]$image.Save(‘./images/_20220819_121454_13GJMM.png’,[System.Drawing.Imaging.ImageFormat]::Png); Write-Output ‘clipboard content saved as file’} else {Write-Output ‘clipboard does not contain image data’}"’ 是我的powerline不支持这里面的哪个符号么?

没看大懂,这几行配置仅限于应用在windows的mingw编译版本,不能用在wsl,Linux发行版,或者mac里

我看你这里面有 usr/bin/bash ,应该不行吧