Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
From an mu4e view, without prompt, save all attachments to a directory and open dired.
;; DEPRECATED - see https://pitch-io.slack.com/archives/CBKNRBRHA/p1630054796006400
(defvar bulk-saved-attachments-dir (expand-file-name "~/Documents/mu4e"))
(defun cleanse-subject (sub)
(replace-regexp-in-string
"[^A-Z0-9]+"
"-"
(downcase sub)))
(defun view-attachments-dired (&optional msg)
"Saves all of the attachments in `msg' to a directory under
`bulk-saved-attachments-dir' which is derived from the subject
beloning to `msg'. Existing filenames will be overwritten without
prompt. The directories are not cleaned up in any way."
(interactive)
(let* ((msg (or msg (mu4e-message-at-point)))
(id (cleanse-subject (mu4e-message-field msg :subject)))
(attachdir (concat bulk-saved-attachments-dir "/" id))
(count (hash-table-count mu4e~view-attach-map)))
(if (> count 0)
(progn
(mkdir attachdir t)
(dolist (num (number-sequence 1 count))
(let* ((att (mu4e~view-get-attach msg num))
(fname (plist-get att :name))
(index (plist-get att :index))
fpath)
(setq fpath (concat attachdir "/" fname))
(mu4e~proc-extract
'save (mu4e-message-field msg :docid)
index mu4e-decryption-policy fpath)))
(dired attachdir)
(revert-buffer))
(message "Nothing to extract."))))
@sje30

This comment has been minimized.

Copy link

@sje30 sje30 commented Aug 30, 2021

Thanks for this -- I've been using this regularly until the recent mu4e updates to 1.6.x which removed a couple of the internal u4e~ functions.

my replacement is https://github.com/sje30/emacs/blob/master/mu4e-view-save-all-attachments.el

@philjackson

This comment has been minimized.

Copy link
Owner Author

@philjackson philjackson commented Aug 30, 2021

@sje30 Thanks! I've put a note at the top of the gist - glad you've found it useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment