Help us understand the problem. What is going on with this article?

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

クリップボードにコピーした画像を出力したいシーンは多々ある。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))
Ladicle
Software Engineer I'm developing Kubernetes as a Service :)
https://ladicle.com
zlab
技術で新しい世界へシフトする。
https://zlab.co.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした