Running Emacs Lisp On Several Files

Xah Lee as an instructive post on converting and scaling images with Emacs. Unlike him, I don’t often need to scale or convert even a single image, let alone many, but he does show an interesting technique to pass several files to an Elisp function. The idea is to use dired to mark the files you’re interested in and then use dired-get-marked-files to pass them to the function.

The template for performing some action on each of the marked files is

(defun process-each-file (files)
  "Perform an action on each file in list FILES."
  (interactive (list (dired-get-marked-files)))
  (mapc (lambda (f) (perform-action-on-file f)) files))

For example, if you want to perform perform-action-on-file on each JPG file you can use 【*..jpg in dired to mark all the JPG files and then call process-each-file.

The trick that makes this work is passing a list to the interactive declaration. I’ve written about that before. This is a handy technique that can often be used to advantage.

This entry was posted in Programming and tagged , . Bookmark the permalink.