Emacs Lisp: take a screenshot withing GNU Emacs and `maim`

Emacs Lisp: take a screenshot withing GNU Emacs and maim

maim is small shell utility that takes screenshots, and with the option -s it allows cropping of a screenshot. This makes it handy to bind the <print> key to screenshot function. After taking screenshot, Emacs is to open the Dired buffer with list of screenshots. From there you may send them by email or do whatever you want. If you are giving it the universal argument C-u 0 it will crop the rectangle on the screen.

This may be very handy when working within the GNU Emacs operating environment and when you wish to take a screenshot and quickly do something with the file.

Update on 2019-08-14

If you are using the maim command just as me then C-u 0 M-x screenshot will give you a croping feature, so you may crop part of the screen. Otherwise C-u NUMBER M-x screenshot will wait for number of seconds and then invoke the screenshot.

(defvar *image-directory* "/home/data1/protected/Media/Pictures/WebScreenshots")

(defun screenshot (&optional crop)
  "Takes screenshots and opens the folder in Dired"
  (interactive "p")
  (let* ((image-capture-program (if (= current-prefix-arg 0) "maim -s" "maim"))
     (sleep-seconds (if (and current-prefix-arg (not (zerop current-prefix-arg)))
            current-prefix-arg 0))
     (image-type ".png")
     (year (format-time-string "%Y"))
     (month (format-time-string "%m"))
     (date (format-time-string "%Y-%d-%m"))
     (directory (concat *image-directory* "/" year "/" month "/" date))
     (filename (concat directory "/" (format-time-string "%Y-%d-%m-%T") image-type))
     (command (concat image-capture-program " \"" filename "\"")))
    (make-directory directory t)
    (sleep-for sleep-seconds)
    (shell-command command)
    (find-file-other-window directory)
    (revert-buffer)))

(global-set-key (kbd "<print>") 'screenshot)

Always sort files by groups, people and chronologically. It will make your life easier.

Leave Your Comment or Contact GNU.Support

Contact GNU.Support now. There is a simple rule at GNU.Support: if we can help you, we do, whenever and wherever necessary, and it's the way we've been doing business since 2002, and the only way we know


Full name:


E-mail:


Message: