作为研究日志,我使用Emacs Org模式。有时我想通过屏幕截图来跟踪某些内容,但我绝对不想将它们保存下来。因此我想知道是否有办法将这些图片插入到我的org模式文件中,就像从剪贴板复制文字一样?
作为研究日志,我使用Emacs Org模式。有时我想通过屏幕截图来跟踪某些内容,但我绝对不想将它们保存下来。因此我想知道是否有办法将这些图片插入到我的org模式文件中,就像从剪贴板复制文字一样?
目前尚未实现您所需的确切功能,但如果您认为“绝对不想保存它们”,我会对将大量图像保存到研究日志中持怀疑态度。
无论如何,您所需的功能在最近几年的org-mode邮件列表中已经被提出了几次 - 请查看:
http://comments.gmane.org/gmane.emacs.orgmode/33770
http://www.mail-archive.com/emacs-orgmode@gnu.org/msg50862.html
第一个链接包括一些代码来启动一个截屏实用程序(通过ImageMagick)来[唯一]保存文件并在您的org-mode缓冲区中插入内联链接。
正如该线程中所述,该代码得到改进,并添加到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))
对于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))
我最近稍微改进了脚本。现在它会将截屏保存在子目录中,如果需要的话会创建该目录,并且只有当图片实际被创建时(即如果按ESC则不执行任何操作),才会将链接插入到emacs中。它还可以在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 "]]"))))
这里的答案集中在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))
xclip -selection clipboard -t image/png -o > file.png
参考链接:https://unix.stackexchange.com/a/145134/118575 - fikovnik[[ ./${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)
)
同时,我发现上面的方法效果不佳。从技术上讲,它是可行的,但它会生成很多与文件名相同的文件夹名称,使得选择文件变得繁琐,因为有些文件夹具有类似的名称。
相反,我采用了一种解决方案,将所有我的 org 文件保存在同一个文件夹中,并创建一个名为“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)
)
#!/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
我的解决方案适用于Windows平台。非常感谢Assem
提供的解决方案,我的解决方案是基于他的。
我编写了一个C#控制台应用程序CbImage2File
,将剪贴板上的图像(如果有)写入到作为命令行参数给出的路径中。您需要引用System.Windows.Forms
程序集。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CbImage2File
{
class Program
{
private static int FAILURE = 1;
private static void Failure(string description)
{
Console.Error.WriteLine("Clipboard does not contain image data");
Environment.Exit(FAILURE);
}
[STAThread]
static void Main(string[] args)
{
var image = System.Windows.Forms.Clipboard.GetImage();
if (image == null)
{
Failure("Clipboard does not contain image data");
}
if (args.Length == 0)
{
Failure("You have not specified an output path for the image");
}
if (args.Length > 1)
{
Failure("You must only specify a file path (1 argument)");
}
var filePath = args[0];
var folderInfo = new System.IO.FileInfo(filePath).Directory;
if (!folderInfo.Exists)
{
System.IO.Directory.CreateDirectory(folderInfo.FullName);
}
image.Save(filePath);
}
}
}
我使用 Greenshot 来截屏,通过其配置,自动将截屏复制到剪贴板。
然后我使用快捷键 C-S-v
,使用函数 org-insert-image-from-clipboard
将图像粘贴到 org 模式缓冲区中:
(defun org-insert-image-from-clipboard ()
(interactive)
(let* ((the-dir (file-name-directory buffer-file-name))
(attachments-dir (concat the-dir "attachments"))
(png-file-name (format-time-string "%a%d%b%Y_%H%M%S.png"))
(png-path (concat attachments-dir "/" png-file-name))
(temp-buffer-name "CbImage2File-buffer"))
(call-process "C:/_code/CbImage2File/CbImage2File/bin/Debug/CbImage2File.exe" nil temp-buffer-name nil png-path)
(let ((result (with-current-buffer temp-buffer-name (buffer-string))))
(progn
(kill-buffer temp-buffer-name)
(if (string= result "")
(progn
(insert (concat "[[./attachments/" png-file-name "]]"))
(org-display-inline-images))
(insert result))))))
(define-key org-mode-map (kbd "C-S-v") 'org-insert-image-from-clipboard)
这个函数会计算出文件路径,然后调用C#应用程序创建png
文件,接着添加图片标签并调用org-display-inline-images
来显示图片。如果剪贴板中没有图像,则会粘贴C#应用程序的响应。
对于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))
(sleep-for 0.5)
,否则ContainsImage()
调用将返回false。但这很棒。 - Dave
call-process
中的import
替换为任何可以指定输出文件名的快照实用程序。 - assemgnome-screenshot --interactive
时,它会在让您选择剪辑区域之前隐藏GUI。希望我表达得清楚。 - Jason