Emacs: 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-06-09"
  (interactive (list (ido-read-directory-name "Move img to dir:" )))
  (let (
        ($p0 (point))
        ($dirs '( "~/Downloads/" "~/Pictures/" "~/Desktop/" "~/Documents/" "~/" "/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
          (let (($x (file-name-extension $fromPath )))
            (if $x
                (downcase $x)
    (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)
              (goto-char $p0)
              (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")
              ;;   ;;
              ;;   )
            (goto-char $p0)
            (insert "\n\n")
            (insert $toPath)
            (insert "\n\n")))))))

Elisp HTML Commands

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