Elisp: Move Image File

By Xah Lee. Date: . Last updated: .

Here's a emacs command that moves image file from a download directory to your blog directory and create a html image link to it.

When you write blog with many images, photo, or screenshots, suppose you just did a screenshot. Then, you have to move the image to your blog's directory, then create a link to the image in your blog. It's a tedious process.

This commands makes it a single command call.

Here's the code.

(defun xah-move-image-file ( @toDirName  )
  "Move image file to another dir.

from directories checked are:

The first file whose name starts with ee or tt or contain “Screenshot”, “Screen Shot” , will be moved.

The destination dir and new file name is asked by a prompt. A random string attached (as id) is added to file name, and any uppercase extension name is lowercased. Space in filename is replaced by the low line char “_”.
If the file name ends in png, “optipng” is called on it.

URL `http://ergoemacs.org/emacs/move_image_file.html'
Version 2019-02-17"
  (interactive (list (read-directory-name "Move img to dir:" )))
  (let (
        ($dirs '( "~/Downloads/" "~/Pictures/" "~/Desktop/" "~/" "/tmp" ))
         (let* (($charset "bcdfghjkmnpqrstvwxyz23456789")
                ($len (length $charset))
                ($randlist nil))
           (dotimes (_ 5)
             (push (char-to-string (elt $charset (random $len)))  $randlist))
           (mapconcat 'identity $randlist ""))))
    (setq $fromPath
          (catch 'TAG
            (dolist (x $dirs )

              (let ((mm (directory-files x t "^ee\\|^tt\\|Screen Shot\\|Screenshot\\|[0-9A-Za-z]\\{11\\}\._[A-Z]\\{2\\}[0-9]\\{4\\}_\.jpg" t)))
                (if mm
                      (throw 'TAG (car mm)))
    (when (not $fromPath)
      (error "no file name starts with ee nor contain “Screen Shot” at dirs %s" $dirs))

    (setq $ext (downcase (file-name-extension $fromPath )))
    (setq $newName1 (file-name-nondirectory (file-name-sans-extension $fromPath)))
    (setq $newName1
           "Screen Shot \\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\) at [0-9]+.[0-9]\\{2\\}.[0-9]\\{2\\} \\(AM\\|PM\\)"
           $newName1 ))
    ;; Screen Shot 2018-07-25 at 2.46.36 AM.png
    (setq $newName1 (read-string "file name:" $newName1 nil $newName1 ))
    (setq $newName1
          (concat (replace-regexp-in-string " " "_" $newName1)
    (setq $toPath (concat (file-name-as-directory @toDirName ) $newName1 $ext))

    (when (string-equal $ext "jpg-large")
      (setq $toPath (concat (file-name-sans-extension $toPath) ".jpg")))
    (when (string-equal $ext "jpg_large")
      (setq $toPath (concat (file-name-sans-extension $toPath) ".jpg")))
    (when (string-equal $ext "jpeg")
      (setq $toPath (concat (file-name-sans-extension $toPath) ".jpg")))
    (message "from path is 「%s」\n to path is 「%s」 " $fromPath $toPath)
    (if (file-exists-p $toPath)
        (error "move to path exist: %s" $toPath)
        (rename-file $fromPath $toPath)
        (when (string-equal (file-name-extension $toPath ) "png")
          (when (eq (shell-command "which optipng") 0)
            (message "optimizing with optipng")
            (shell-command (concat "optipng " $toPath " &"))))
        (when (string-equal major-mode "dired-mode")
        (if (string-equal major-mode "xah-html-mode")
              (kill-new $toPath)
              (insert "\n\n")
              (insert $toPath)
              (insert "\n\n")
              (backward-word )
              (let ($p1 $p2 $altStr)
                (setq $altStr (xah-html-image-linkify))
                (search-backward "<img ")
                (insert "<figure>\n")
                (search-forward ">")
                (insert "\n<figcaption>\n")
                (insert $altStr "\n</figcaption>\n</figure>\n\n")
            (insert "\n\n")
            (insert $toPath)
            (insert "\n\n")))))))

Elisp HTML Commands

  1. wrap-url
  2. Make URL links
  3. Make Image Links
  4. Extract URL
  5. Word to Wikipedia Linkify
  6. Wikipedia URL Linkify
  7. URL Percent Decode/Encode
  8. Lines to HTML Table
  9. Markup Function Names
  10. Color Source Code
  11. Transform Text Under Cursor
  12. Chinese Char Reference Linkify
  13. HTML make-citation
  14. update-title
  15. Google Map Linkify 🌐
  16. CSS Compressor
  17. Make Ruby Annotation
  18. Move Image File

If you have a question, put $5 at patreon and message me.
Or Buy Xah Emacs Tutorial
Or buy a nice keyboard: Best Keyboards for Emacs


Emacs Lisp