Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
AtomicNess123 opened this issue Apr 3, 2022 · 13 comments

Comments

@AtomicNess123
Copy link

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")

@ghost
Copy link

ghost commented Apr 26, 2022

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)

@AtomicNess123
Copy link
Author

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

@ghost
Copy link

ghost commented Apr 26, 2022

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.

@AtomicNess123
Copy link
Author

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?

@ghost
Copy link

ghost commented Apr 26, 2022

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.

@AtomicNess123
Copy link
Author

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

@ghost
Copy link

ghost commented Apr 27, 2022

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))))))

@AtomicNess123
Copy link
Author

Yes! My bad! Thanks again!

@AtomicNess123
Copy link
Author

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
Copy link
Author

Any advice on this? Thanks!

@quarkquartet
Copy link

@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?

@dvorak0
Copy link

dvorak0 commented Jan 30, 2023

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)

@danielkrajnik
Copy link

danielkrajnik commented Aug 2, 2023

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants