Skip to content

Instantly share code, notes, and snippets.

@BrandonIrizarry
Created February 21, 2023 16:06
Show Gist options
  • Save BrandonIrizarry/a8e9c53c3e329c7014e55bc9db84a604 to your computer and use it in GitHub Desktop.
Save BrandonIrizarry/a8e9c53c3e329c7014e55bc9db84a604 to your computer and use it in GitHub Desktop.
(defun bci/--screenshot-to-image-format (type)
"Save a screenshot of the current frame as a TYPE image, with
the appropriate file-extension.
TYPE should be given as a quoted symbol, as it is passed directly
to `x-export-frames'.
The result is saved to a temp file, with the filename placed also
in the kill-ring."
(let* ((file-extension
(if (eq type 'postscript)
".ps"
(format ".%s" (symbol-name type))))
(filename (make-temp-file "Emacs" nil file-extension))
(data (x-export-frames nil type)))
(with-temp-file filename
(insert data))
(kill-new filename)
(message filename)))
;; Wrapper for convenient interactive usage
(defun bci/screenshot-to-image-format ()
(interactive)
(let* ((table '(("svg" . svg)
("pdf" . pdf)
("postscript" . postscript)
("png" . png)))
(choice
(cdr (assoc (completing-read "Format: "
'("svg" "pdf" "postscript" "png") nil)
table))))
(let ((filename (bci/--screenshot-to-image-format choice)))
(if (y-or-n-p "Preview?")
(find-file filename)))))
(bind-key "C-<print>" #'bci/screenshot-to-image-format)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment