推荐给大家在org-mode中快速插入截图的一小段脚本:写org-mode很好用

  1. 安装snipaste截图软件(windows下,linux下使用flameshot/snippaste都可以);
  2. 在脚本中设置安装路径/或者系统里面环境变量应该也可以;
  3. 截图会直接插入到org-mode文件中,并且会在文件同样的目录下创建一个文件夹,并按照时间格式存储图片;

另外: 建议尽量不要使用中文文件名:因为创建的文件夹如果是中文,有可能window系统下snippaste识别成了乱码,如果需要使用中文,可能需要设置一下对用的路径的编码格式;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;org do screenshots and insert to org files
;;; windows use tool:snipaster
;;; linux   use tool:flameshot

(defvar v-snip-exe-path "C:\\tools\\Snipaste\\Snipaste.exe")
;;if path-name has space
;;(defvar v-snip-exe-path "C:/\"Program Files\"/Snipaste/Snipaste.exe")

(defun v-org-snip-insert ()
  "Take a screenshot into a unique-named file in the current buffer file
   directory and insert a link to this file."
  (interactive)
  (lower-frame)
  (let* ((capture-name (concat (format-time-string "%Y%m%d%H%M%S") ".png"))
         (capture-imag-dir (concat "_images_" (file-name-nondirectory buffer-file-name) "/"))
         (capture-save-path (concat (file-name-directory buffer-file-name) capture-imag-dir))
         (capture-file (concat capture-save-path capture-name))
         (v-org-file-path 
          (if (eq system-type `windows-nt)
               (encode-coding-string (replace-regexp-in-string "/" "\\\\" capture-file) 'gbk)
               capture-file
               )))
  (progn (message v-org-file-path)
         (unless (file-exists-p  capture-save-path)
             (make-directory capture-save-path))
          (if (eq system-type `windows-nt)
            (progn ;;; if windows 
                 (shell-command (concat v-snip-exe-path " snip -o \"" v-org-file-path "\""))) ;;
            (progn ;;; if linux
                 (shell-command (concat  "flameshot gui -p " v-org-file-path )))) ;;

       ;;(insert "#+CAPTATION: <typing in>  \n")
       ;;(insert "#+LABEL:  \n")
         (insert (concat "[[./" capture-imag-dir capture-name "]]"))
       ;;(org-display-inline-images)
	 )))



(defun v-org-snip-del ()
  "del org link and its file  "
  (interactive)
  (beginning-of-line)
  (search-forward "[[")
  (setq path-string-begin (point))
  (search-forward "png")
  (setq path-string-end (point))
  (setq v-line-path-get
	(replace-regexp-in-string "/" "\\\\"
	  (buffer-substring path-string-begin path-string-end)))
  (if (eq system-type `windows-nt)
      (shell-command (concat "del " v-line-path-get))
      (shell-command (concat "rm -rf " v-line-path-get)))
  (beginning-of-line)
  (kill-line))


(defun v-org-snip-del-region ()
  "del org link and its file  "
  (interactive)
  (narrow-to-region (region-beginning) (region-end))
  (while (search-forward-regexp "\[\[.*_images.*org.*png\]\]")
    (v-org-snip-del))
  ;;(widen)
)

(global-set-key (kbd "C-c p s") 'v-org-snip-insert)

2 个赞

Since Org 9.7, you can yank images from clipboard using M-x yank-media. With flameshot specifically, you can select region and press C-c to copy the region to clipboard.


自Org 9.7版本开始,您可以使用M-x yank-media命令从剪贴板粘贴图像。 特别是在flameshot中,您可以选择区域,然后按C-c将该区域复制到剪贴板。

楼主的方案是用在Windows上的,yank-media目前在windows上不可用。

忘了之前在哪抄的了,用的 powershell

  ;; 支持 Windows 从剪贴板复制图片
  (defun freedom/insert-image-from-clipboard ()
    "Insert an image from the clipboard into the current org buffer."
    (interactive)
    (let* ((current-dir (file-name-directory buffer-file-name))
           (file-name-base (file-name-base buffer-file-name))
           (attach-dir (concat current-dir "attach/" file-name-base "/"))
           ;; (attach-dir (concat current-dir "attach/"  (file-name-nondirectory buffer-file-name) "/"))
           (image-file (concat attach-dir (format-time-string "%Y%m%d_%H%M%S") ".png")))
      ;; Ensure attach directory exists
      (unless (file-exists-p attach-dir)
        (make-directory attach-dir t))
      ;; Save the clipboard image to the attach directory
      (if (eq system-type 'windows-nt)
          (progn 
            (shell-command (concat "powershell -command \"Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.Clipboard]::GetImage().Save('" image-file "', [System.Drawing.Imaging.ImageFormat]::Png)\""))
            ;; 转换格式, 从 c:/Users/Jack/Desktop/attact/test/clipboard.png 之类
            ;; 转换成 ./attach/test/clipboard.png
            (setq image-file (replace-regexp-in-string
                              "^[^:]+:/.*\\(/attach/.*\\)" ".\\1" image-file))
            (when (eq major-mode 'org-mode)
              ;; 输入文件所在位置文本
              (insert (concat "[[file:" image-file "]]"))
              ;; 显示图片
              (org-display-inline-images))
            (when (eq major-mode 'markdown-mode)
              (insert (concat "![]" "(" image-file ")"))
              (markdown-display-inline-images))
            )
        (error "Unsupported OS"))
      ))