2022-08-27 Screenshots from Emacs

Some time ago there was a discussion on the Emacs mailing list about making screenshots from Emacs. From one of the posts there I learned about the x-export-frames function, whose existence is fascinating for me. It basically allows you to make a screenshot of your Emacs frame without ay external program, in one of several formats (including pdf and png). Jean Louis provided some simple code in that post which I reproduce here (with minor changes). The frameshot command takes a picture of your current Emacs frame, puts is in the frameshot-directory directory and moves the point to its line.

(defvar frameshot-directory "~/Pictures/Screenshots/"
  "Default directory for frame shots.")

(defvar frameshot-format 'png
  "Default frame shot format.")

(defun frameshot ()
  "Save Emacs frame as frame shot.
Directory is determined by variable `frameshot-directory' and if
not defined, it will be saved in the `$HOME' directory."
  (interactive)
  (let* ((image (x-export-frames nil (or frameshot-format 'png)))
	 (base-directory (or frameshot-directory (getenv "HOME")))
	 (directory (concat (file-name-as-directory base-directory)
			    (format-time-string "%Y/%m/%Y-%m-%d/")))
	 (file (concat directory (format-time-string "Screenshot-%Y-%m-%d-%T.")
		       (symbol-name frameshot-format))))
    (make-directory directory t)
    (with-temp-file file
      (insert image))
    (dired directory)
    (revert-buffer)
    (dired-goto-file (expand-file-name file))
    (message "Frame shot saved as `%s'" file)))

Even after 20 years Emacs never ceases to astonish me!

CategoryEnglish, CategoryBlog, CategoryEmacs