Paste an image on clipboard to Emacs Org mode file without saving it

后端 未结 8 1003
慢半拍i
慢半拍i 2021-01-30 09:21

As I\'m using the Emacs Org mode as a research log, sometime I want to keep track of something via screenshot images, and I definitely don\'t want to save them. So I\'m wonderin

8条回答
  •  北海茫月
    2021-01-30 09:41

    For OS X users. The following compliments Assem's answer.

    The import command was not working for me, so I swapped it to screencapture with an -i argument. I also find it more convenient to use a relative path for the link rather than the absolute.

    (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))
    

    Update: Improvements

    I have recently improved on the script a little. It now places screenshots in a subdirectory, creates the directory if required, and only inserts the link into emacs if the image was actually created (i.e., does nothing if you hit ESC). It also works on both Ubuntu and 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 "]]"))))
    

提交回复
热议问题