bburns / clipmon

Clipboard monitor for Emacs - monitors clipboard and pastes contents on change
95 stars 5 forks source link

Could you extend it to recognize images? #24

Closed gnusupport closed 2 days ago

gnusupport commented 2 days ago

I'm looking for a way to paste images directly from the clipboard into Emacs. If an image is detected, I would like to perform various operations on it, such as saving it to a file or recording it.

This package looks like it could do something like that.

gnusupport commented 2 days ago
(defun hyperscope-yank-media-handler (mime-type data)
  "HANDLER function to be registered with `yank-media-handler'."
  (let* ((extension (cond ((and (string-match "image" (symbol-name mime-type))
                (string-match "jpeg" (symbol-name mime-type)))
               ".jpg")
              ((and (string-match "image" (symbol-name mime-type))
                (string-match "png" (symbol-name mime-type)))
               ".png")
              (t (rcd-warning-message "No option for MIME type: %s" (symbol-name mime-type)))))
     (data-file (concat (file-name-as-directory (getenv "TMPDIR")) "TEMP-IMAGE-" (rcd-timestamp-date-time) "-" (rcd-random-word 4) extension)))
    (string-to-file-force data data-file)
    (when (hyperscope-store-files (hyperscope-user-screenshots-set) (list data-file))
      (let ((last (hyperscope-last-id)))
    (rcd-db-update-entry "hyobjects" "hyobjects_hyobjectsubtypes" last 150 rcd-db)
    (hyperscope-isolate-last)))
    (rcd-message "Stored %s as file %s" (symbol-name mime-type) data-file)))

(defun hyperscope-capture-clipboard-media ()
  "Capture any clipboard image into the Hyperscope Dynamic Knowledge Repository."
  (interactive)
  (select-frame-set-input-focus (selected-frame))
  (with-temp-buffer
    (yank-media-handler "image/.*" 'hyperscope-yank-media-handler)
    (yank-media)))

I have solved it for me, with that built-in function now, and I can tie it to key shortcut in X Window Manager, it works well.