LoginSignup
6
8

More than 3 years have passed since last update.

クリップボードの画像を出力するpngpasteコマンドとEmacsの連携 (macOS)

Last updated at Posted at 2020-05-29

クリップボードにコピーした画像を出力したいシーンは多々ある。Preview使うとか方法はさまざまだが、Alfredとかターミナルとかから手軽に使うためにはコマンドになってると便利だ。そんなこんなでおすすめなのはpngpaste。インストール方法としてbrewもサポートしてる。

brew install pngpaste

画像のドラック&ドロップ等、Emacsでは殆どのケースをorg-downloadで解決できるが、クリップボードの画像貼り付けはサポートされていない。
そこで、次のようにpngpasteを呼び出す関数を定義して、C-M-yあたりにバインドすると...Emacsのorg-modeでinline-imageを楽に挿入できる。(便利!

(with-eval-after-load "org"
  (defun org-insert-clipboard-image ()
    "Generate png file from a clipboard image and insert a link to current buffer."
    (interactive)
    (let* ((filename
            (concat (file-name-nondirectory (buffer-file-name))
                    "_image/"
                    (format-time-string "%Y%m%d_%H%M%S")
                    ".png")))
      (unless (file-exists-p (file-name-directory filename))
        (make-directory (file-name-directory filename)))
      (shell-command (concat "pngpaste " filename))
      (if (file-exists-p filename)
          (insert (concat "[[file:" filename "]]")))
      (org-display-inline-images)))

  (global-set-key (kbd "C-M-y") 'org-insert-clipboard-image))
6
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
8