org-download icon indicating copy to clipboard operation
org-download copied to clipboard

Syntax to create subdirectories for each org file: (setq-default org-download-image-dir "./img/?")

Open AtomicNess123 opened this issue 3 years ago • 13 comments

What would be the syntax to create separate directories withint an /img directory for each orgmode buffer? This would allow a cleaner organization of images (not having all images in one directory).

This creates a directory /img where all images would go:

(setq-default org-download-image-dir "./img")

AtomicNess123 avatar Apr 03 '22 18:04 AtomicNess123

Duplicate of #46

A slightly better function that creates separate folders for each org file.

(defun my-org-download-method (link)
    (let ((filename
           (file-name-nondirectory
            (car (url-path-and-query
                  (url-generic-parse-url link)))))
          (dirname (concat "./img/" (file-name-sans-extension (file-name-nondirectory (buffer-file-name))))))
      (setq org-download-image-dir dirname)
      (make-directory dirname t)
      (expand-file-name filename dirname)))
  (setq org-download-method 'my-org-download-method)

ghost avatar Apr 26 '22 06:04 ghost

Thanks! Haven't tried it yet. What names would the directories within img/ have?

AtomicNess123 avatar Apr 26 '22 08:04 AtomicNess123

Thanks! Haven't tried it yet. What names would the directories within img/ have?

The current org buffer file name where you're inserting the image.

ghost avatar Apr 26 '22 08:04 ghost

Thank you! So at the moment in my init I have these two lines regarding org-download:

(setq org-download-method 'directory)
(setq-default org-download-image-dir "./img")

Shall I replace both with just

(setq org-download-method 'my-org-download-method)

I suppose the line (setq-default org-download-image-dir "./img") is not necessary?

AtomicNess123 avatar Apr 26 '22 09:04 AtomicNess123

Thank you! So at the moment in my init I have these two lines regarding org-download:

(setq org-download-method 'directory)
(setq-default org-download-image-dir "./img")

Shall I replace both with just

(setq org-download-method 'my-org-download-method)

You would include the function defintion for my-org-download-method as well before it.

I suppose the line (setq-default org-download-image-dir "./img") is not necessary?

Yes, not necessary.

ghost avatar Apr 26 '22 16:04 ghost

I just tried this but it doesn't create subdirectories within /img eg., /img/NAMEOFBUGGER/. It creates new directories with name /imgNAMEOFBUFFER.

AtomicNess123 avatar Apr 27 '22 08:04 AtomicNess123

I just tried this but it doesn't create subdirectories within /img eg., /img/NAMEOFBUGGER/. It creates new directories with name /imgNAMEOFBUFFER.

Ensure that there is a trailing slash after "./img/" in this line

(dirname (concat "./img/" (file-name-sans-extension (file-name-nondirectory (buffer-file-name))))))

ghost avatar Apr 27 '22 08:04 ghost

Yes! My bad! Thanks again!

AtomicNess123 avatar Apr 27 '22 08:04 AtomicNess123

Hi, I encountered an issue with this: now my screenshots are named screenshot.png. So if I paste a screenshot using org-download in my org buffer untitled.org, it creates a folder /img/untitled/screenshot.png.

So when I paste a second screenshot... it tells me there is already a screenshot.png.

Before it used to name every screenshot prepending the date plus time: 20220418-142539_screenshot.png. How could your code be interfering with this naming? Thanks!

AtomicNess123 avatar May 03 '22 16:05 AtomicNess123

Any advice on this? Thanks!

AtomicNess123 avatar May 10 '22 21:05 AtomicNess123

@pirattidasan Thank you.

Tried you code. It works. However, the display of image size changes.

In the default setting where the org-download-method is attach, the dragged-in image is displayed in a very proper size that matches the buffer font and width. If I use your code and change this method, the image size is displayed very large.

I'm using doom emacs. I'm not sure whether the image size is defined in doom or by the org-attach itself. Some idea about this?

quarkquartet avatar May 23 '22 00:05 quarkquartet

Hi, I encountered an issue with this: now my screenshots are named screenshot.png. So if I paste a screenshot using org-download in my org buffer untitled.org, it creates a folder /img/untitled/screenshot.png.

So when I paste a second screenshot... it tells me there is already a screenshot.png.

Before it used to name every screenshot prepending the date plus time: 20220418-142539_screenshot.png. How could your code be interfering with this naming? Thanks!

org-download-file-format-function should help.

 (defun my-org-download-method (link)
       (let ((filename
              (file-name-nondirectory
               (car (url-path-and-query
                     (url-generic-parse-url link)))))
             (dirname (concat "./org-download-imgs/" (file-name-sans-extension (file-name-nondirectory       (buffer-file-name))))))
         (setq org-download-image-dir dirname)
         (make-directory dirname t)
         (expand-file-name (funcall org-download-file-format-function filename) dirname)))
     (setq org-download-method 'my-org-download-method)

dvorak0 avatar Jan 30 '23 06:01 dvorak0

doesn't work

(defun my-org-download-method (link)
    (let ((dirname (concat (file-name-sans-extension (buffer-file-name)) "-images")))
      (setq org-download-image-dir dirname)
      (make-directory dirname t)
      ))
  (setq org-download-method 'my-org-download-method)

danielkrajnik avatar Aug 02 '23 14:08 danielkrajnik