Emacs: Move Image File

By Xah Lee. Date: . Last updated: .

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

You can customize which directories to search, and a list of regex to match the file you want to move.

This command is useful if you write a blog and often want to include screenshots or photos you have taken, or images from internet.

(defvar xah-move-image-file-from-dirs nil "A list of dir paths used by `xah-move-image-file' to search for temp image files.")
(setq xah-move-image-file-from-dirs
      '(
        "~/Downloads/"
        "~/Pictures/"
        "~/Desktop/"
        ;; "~/Documents/"
        "~/"
        "/tmp"
        ))

(defvar xah-move-image-file-regex-list nil "A list of regex strings used by `xah-move-image-file' to search for temp image files.")
(setq xah-move-image-file-regex-list
      '(
        "^tt"
        "^IMG_"
        "^Screen Shot"
        "^Screenshot"
        "^screenshot"
        "[+0-9A-Za-z]\\{11\\}\._[A-Z]\\{2\\}[0-9]\\{4\\}_\.jpg" ; amazon pic
        "[+0-9A-Za-z]\\{11\\}\._[A-Z]\\{2\\}_[A-Z]\\{2\\}[0-9]\\{4\\}_\.jpg" ; amazon pic
        ;; 711UVfyz+FL._AC_SL1500_.jpg
        ))

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

Search a list of dirs by a list of regex. If a match is found, move it into a new file path, by prompting user to type it.

The from dirs is defined in `xah-move-image-file-from-dirs'
The regex list is defined in `xah-move-image-file-regex-list'

A random string attached (as id) is added to file name, and any uppercase file extension name is lowercased, e.g. .JPG becomes .jpg. Space in filename is replaced by the low line char “_”.

Automatically call `xah-dired-remove-all-metadata' and `xah-dired-optimize-png' afterwards.

URL `http://ergoemacs.org/emacs/move_image_file.html'
Version 2019 2021-07-04"
  (interactive (list (ido-read-directory-name "Move img to dir:" )))
  (let ($p0 $dirs $fromPath $newName1 $ext $toPath $randStr)
    (setq $p0 (point))
    (setq $dirs (if xah-move-image-file-from-dirs
                    xah-move-image-file-from-dirs
                  '( "~/Downloads/" "~/Desktop/" "/tmp" )))
    (setq $randStr
          (let ( $charset $len $randlist )
            (setq $charset "bcdfghjkmnpqrstvwxyz23456789BCDFGHJKMNPQRSTVWXYZ")
            (setq $len (length $charset))
            (setq $randlist nil)
            (dotimes (_ 5)
              (push (char-to-string (elt $charset (random $len)))  $randlist))
            (mapconcat 'identity $randlist "")))
    (setq $fromPath
          (catch 'found57804
            (dolist (xdir $dirs )
              (when (file-exists-p xdir)
                (let (($dirFiles
                       (directory-files
                        xdir t
                        (mapconcat 'identity
                                   (if xah-move-image-file-regex-list
                                       xah-move-image-file-regex-list
                                     '(
                                       "^IMG_"
                                       "^Screen Shot"
                                       "^Screenshot"
                                       "^screenshot"
                                       ))
                                   "\\|"))))
                  (when $dirFiles (throw 'found57804 (car $dirFiles))))))))
    (when (not $fromPath)
      (error "No file name matched regexes %s at dirs %s" xah-move-image-file-regex-list $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 (replace-regexp-in-string "^tt[0-9]*" "" $newName1 ))
    (setq $newName1
          (replace-regexp-in-string
           "Screen Shot \\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\) at [0-9]+.[0-9]\\{2\\}.[0-9]\\{2\\} \\(AM\\|PM\\)"
           "screenshot_\\1"
           $newName1 ))
    ;; Screen Shot 2018-07-25 at 2.46.36 AM.png
    (setq $newName1 (read-string "new name:" $newName1 nil $newName1 ))
    (setq $newName1
          (concat
           (replace-regexp-in-string "^tt\\|,\\| \\|\\+\\|(\\|)" "_" $newName1)
           "_"
           $randStr
           "."
           ))
    (setq $toPath (concat (file-name-as-directory @toDirName ) $newName1 $ext))
    (when (or (string-equal $ext "jpg-large")
              (string-equal $ext "jpg_large")
              (string-equal $ext "jpg_medium")
              (string-equal $ext "jpeg")
              (string-equal $ext "jfif"))
      (setq $toPath (concat (file-name-sans-extension $toPath) ".jpg")))
    (message "xah-move-image-file: from path is 「%s」\n to path is 「%s」 " $fromPath $toPath)
    (if (file-exists-p $toPath)
        (error "move to path exist: %s" $toPath)
      (progn
        (rename-file $fromPath $toPath)
        (when (string-equal major-mode "dired-mode")
          (revert-buffer))
        (if (string-equal major-mode "xah-html-mode")
            (progn
              (kill-new $toPath)
              (goto-char $p0)
              (insert "\n\n")
              (insert $toPath)
              (insert "\n\n")
              (backward-word )
              (xah-html-any-linkify))
          (progn
            (goto-char $p0)
            (insert "\n\n")
            (insert $toPath)
            (insert "\n\n")))
        (when (fboundp 'xah-dired-remove-all-metadata)
          (xah-dired-remove-all-metadata (list $toPath)))
        (when (and (string-equal $ext "png") (fboundp 'xah-dired-optimize-png))
          (xah-dired-optimize-png (list $toPath)))))))

you probably also want to install Emacs: xah-dired.el

HTML

Tags

Links

CSS

Misc

If you have a question, put $5 at patreon and message me on xah discord.
Or support me by Buy Xah Emacs Tutorial