将剪贴板上的图像粘贴到Emacs Org模式文件而不保存

lin*_*ina 23 emacs screenshot org-mode

由于我使用Emacs Org模式作为研究日志,有时我想通过截图图像跟踪某些内容,我绝对不想保存它们.所以我想知道有没有办法将这些数字插入到我的组织模式文件中,就像从剪贴板中处理它们一样?

ass*_*sem 20

您想要的确切功能目前尚未实现,但如果您认为"绝对不想保存它们",我会怀疑将大量图像保存到研究日志中.

无论如何,你想要的功能近年来已经在组织模式邮件列表中表达了几次 - 检查

http://comments.gmane.org/gmane.emacs.orgmode/33770

http://www.mail-archive.com/emacs-orgmode@gnu.org/msg50862.html

第一个链接包含一些代码,用于启动屏幕截图实用程序(通过ImageMagick)以[唯一]保存文件并在组织模式缓冲区中插入内联链接.

正如该线程所述,代码已经改进并添加到org-mode hacks页面 - 它有很多有用的宝石:

http://orgmode.org/worg/org-hacks.html

(defun my-org-screenshot ()
  "Take a screenshot into a time stamped unique-named file in the
same directory as the org-buffer and insert a link to this file."
  (interactive)
  (setq filename
        (concat
         (make-temp-name
          (concat (buffer-file-name)
                  "_"
                  (format-time-string "%Y%m%d_%H%M%S_")) ) ".png"))
  (call-process "import" nil nil nil filename)
  (insert (concat "[[" filename "]]"))
  (org-display-inline-images))
Run Code Online (Sandbox Code Playgroud)


Gil*_*lly 11

对于OS X用户.以下是对Assem的回答的赞美.

import命令对我不起作用,所以我把它换成screencapture了一个-i参数.我还发现使用链接的相对路径而不是绝对路径更方便.

(defun my-org-screenshot ()
  "Take a screenshot into a time stamped unique-named file in the
same directory as the org-buffer and insert a link to this file."
  (interactive)
  (setq filename
        (concat
         (make-temp-name
          (concat (file-name-nondirectory (buffer-file-name))
                  "_"
                  (format-time-string "%Y%m%d_%H%M%S_")) ) ".png"))
  (call-process "screencapture" nil nil nil "-i" filename)
  (insert (concat "[[./" filename "]]"))
  (org-display-inline-images))
Run Code Online (Sandbox Code Playgroud)

更新:改进

我最近对脚本进行了一些改进.它现在将屏幕截图放在一个子目录中,如果需要的话创建目录,并且只有在实际创建图像时才将链接插入到emacs中(即,如果你点击ESC则什么都不做).它也适用于Ubuntu和OS X.

(defun my-org-screenshot ()
  "Take a screenshot into a time stamped unique-named file in the
same directory as the org-buffer and insert a link to this file."
  (interactive)
  (org-display-inline-images)
  (setq filename
        (concat
         (make-temp-name
          (concat (file-name-nondirectory (buffer-file-name))
                  "_imgs/"
                  (format-time-string "%Y%m%d_%H%M%S_")) ) ".png"))
  (unless (file-exists-p (file-name-directory filename))
    (make-directory (file-name-directory filename)))
  ; take screenshot
  (if (eq system-type 'darwin)
      (call-process "screencapture" nil nil nil "-i" filename))
  (if (eq system-type 'gnu/linux)
      (call-process "import" nil nil nil filename))
  ; insert into file if correctly taken
  (if (file-exists-p filename)
    (insert (concat "[[file:" filename "]]"))))
Run Code Online (Sandbox Code Playgroud)


Leo*_*sev 7

(如果使用),我使用了@ assem的代码(谢谢v.much btw)并创建了一个版本,而是创建了一个子文件夹(如果它不存在),如:$ {filename} IMG/Then放置一个图像像img_date.png到该文件夹​​然后将相对路径插入缓冲区,如:
[[ ./${filename}IMG/img_2015...png ]]

这是代码:

(defun my-org-screenshot ()
"Take a screenshot into a time stamped unique-named file in the
sub-directory (%filenameIMG) as the org-buffer and insert a link to this file."
(interactive)

(setq foldername (concat (buffer-file-name) "IMG/"))
(if (not (file-exists-p foldername))
  (mkdir foldername))

(setq imgName (concat "img_" (format-time-string "%Y_%m_%d__%H_%M_%S") ".png"))
(setq imgPath (concat (buffer-file-name) "IMG/" imgName))

(call-process "import" nil nil nil imgPath)

(setq relativeFilename (concat "./"
    (buffer-name) "IMG/" imgName))

(insert (concat "[[" relativeFilename "]]"))
(org-display-inline-images)
)
Run Code Online (Sandbox Code Playgroud)

[编辑2015.04.09]

与此同时,我发现上述方法效果不佳.从技术上讲它可以工作,但它会生成许多与文件名匹配的文件夹名称,这使得选择文件变得很繁琐,因为存在具有相似名称的文件夹.

相反,我选择了一个解决方案,我将所有的组织文件保存在同一个文件夹中,并在我的所有图像中都有一个"img"子文件夹.

我使用这样的代码来捕获图像:

(defun my/img-maker ()
 "Make folder if not exist, define image name based on time/date" 
  (setq myvar/img-folder-path (concat default-directory "img/"))

  ; Make img folder if it doesn't exist.
  (if (not (file-exists-p myvar/img-folder-path)) ;[ ] refactor thir and screenshot code.
       (mkdir myvar/img-folder-path))

  (setq myvar/img-name (concat "img_" (format-time-string "%Y_%m_%d__%H_%M_%S") ".png"))
  (setq myvar/img-Abs-Path (concat myvar/img-folder-path myvar/img-name)) ;Relative to workspace.

  (setq myvar/relative-filename (concat "./img/" myvar/img-name))
  (insert "[[" myvar/relative-filename "]]" "\n")
)

(defun my/org-screenshot ()
  "Take a screenshot into a time stamped unique-named file in the
 sub-directory (%filenameIMG) as the org-buffer and insert a link to this file."
  (interactive)
  (my/img-maker)
  ;(make-frame-invisible)
  (lower-frame)
  (call-process "import" nil nil nil myvar/img-Abs-Path)

  (raise-frame)
  ;(make-frame-visible)
  (org-display-inline-images)
)
Run Code Online (Sandbox Code Playgroud)

和我的org/img文件夹中的一个小bash脚本来存档未引用的图像:

#!/bin/sh 

cd ~/git/LeoUfimtsev.github.io/org/img/
mkdir ~/git/LeoUfimtsev.github.io/org/img/archive
FILES=~/git/LeoUfimtsev.github.io/org/img/*.png
for f in $FILES
do
  var_grep_out=$(grep --directories=skip $(basename $f) ../*)
  if [ -z "$var_grep_out" ];
  then 
     echo "Archiving: $f"
     mv $f archive/
  fi
done
Run Code Online (Sandbox Code Playgroud)


小智 6

org -download包就是为此而设计的。


小智 5

我喜欢将属于一个组织文件的图像和其他材料放在一个明确关联的目录中。我为此使用了 org-attachment 机制,并在几年前写了一个包org-attachment-screenshot,它也可以从 MELPA 获得。它允许为附件提供定义目录名称(例如文档名称+一些后缀)的功能,并且还将支持标准附件功能,例如,如果您想根据条目定义附件,并在条目内继承。还为要执行的快照命令提供可定制的变量。请看一看。


ded*_*ed7 5

这里的答案集中在启动emacs中的过程。在macOS上,另一种方法是使用cmd-ctl-shift-4将屏幕快照图像放置在系统剪贴板上,然后返回Emacs并使用emacs lisp调用pngpaste将图像从剪贴板写入文件,然后执行任何操作您需要使用图像文件(插入org,LaTeX或作为本机Emacs内联图像等)。

例如,要插入org:

(defun org-insert-clipboard-image (&optional file)
  (interactive "F")
  (shell-command (concat "pngpaste " file))
  (insert (concat "[[" file "]]"))
  (org-display-inline-images))
Run Code Online (Sandbox Code Playgroud)


Sas*_*ibe 5

对于 Windows 10 用户,我修改了 @assem 提供的答案以使用开箱即用的工具:

(defun my-org-screenshot ()
  "Take a screenshot into a time stamped unique-named file in the
   same directory as the org-buffer and insert a link to this file."  
   (interactive)
   (setq filename
     (concat
       (make-temp-name
         (concat (buffer-file-name)
              "_"
              (format-time-string "%Y%m%d_%H%M%S_")) ) ".png"))
   (shell-command "snippingtool /clip")
   (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))
Run Code Online (Sandbox Code Playgroud)

完整的文章可以在这里找到