1

Suppose following org-file (some_random_name.org) that has 2 images with inserted from org-download.

* heading 1
#+DOWNLOADED: /tmp/xls/xl/media/image2_stage2.png @ 2023-02-24 17:36:38
[[file:heading_1/2023-02-24_17-36-38_image2_stage2.png]]

* Second heading 2

#+DOWNLOADED: /tmp/xls/xl/media/image2_stage4.png @ 2023-02-24 17:37:06
[[file:Second_heading_2/2023-02-24_17-37-06_image2_stage4.png]]

By default, org-download puts these 2 images as following:

.
├── heading_1
│   └── 2023-02-24_17-36-38_image2_stage2.png
├── Second_heading_2
│   └── 2023-02-24_17-37-06_image2_stage4.png
└── some_random_name.org

This is great structure. However, all of my org-files exist in same directory. Consequently, it's structure is a mess. (can't find which directories are associated with which org-file.)

Question How can I make directories created by org-download into single parent directory (with same name as the org-file) ? (i.e. following structure):

.
├── some_random_name
│   ├── heading_1
│   │   └── 2023-02-24_17-36-38_image2_stage2.png
│   └── Second_heading_2
│       └── 2023-02-24_17-37-06_image2_stage4.png
└── some_random_name.org
3
  • Looks like you can set the org-download-image-dir variable for that. To set it per file, you could set it as a file variable... Feb 24, 2023 at 10:02
  • 1
    "... all of my org-files exist in same directory": there is no reason for all Org mode files to be in one directory, so you may rethink that (dis-)organization a bit. Most people organize them per project, There may be a file in each project that contributes to the agenda and it may be a good idea to put that file (or a link to it) in a single directory so that org-agenda-files can be populated easily; but for other files, e.g. docs, images, references, data files, computations, etc., I think the per-project organization wins hands down. And you can still use @dalanicolai's answer in each.
    – NickD
    Feb 24, 2023 at 15:04
  • @NickD I use org-roam, by default it saves all files into same directory (and it seems changing that makes too much work)
    – Garid
    Feb 25, 2023 at 5:53

2 Answers 2

2

I guess you would prefer to set this via a per-directory local-variable. They can be configured like file variables.

In summary, you probably want to create a .dir-locals.el file in your org files directory and store the following code in there:

((org-mode . ((eval . (setq org-download-image-dir (concat "./" (file-name-base buffer-file-name)))))))

Now for each org file you open in that directory, the local value of the org-download-image-dir automatically gets set to the file-name-base (see C-h f file-name-base).

I am not sure how the directory name should be provided. Here I am concating "./" and (file-name-base buffer-file-name), because the org-download-image-dir docstring says that "." is the default value. Othwerise, I guess you can simply wrap the concat form in an expand-file-name.

1
  • 1
    Thank you very much, 2-answers to my 2 questions. I really appreciate it.
    – Garid
    Feb 25, 2023 at 6:04
1

If you want to apply this everywhere, add the following hook into your init.el file:

    (defun my-org-download-set-dir ()
        "Set `org-download-image-dir` to the directory of the current 
        buffer's file."
        (setq-local org-download-image-dir (concat (file-name-directory 
        (buffer-file-name)) "/images/" (file-name-base buffer-file-name) "/") ))
    
    (add-hook 'org-mode-hook 'my-org-download-set-dir)
     

Or if using use-package:

(use-package org-download
      :ensure t
      :config
    (defun my-org-download-set-dir ()
        "Set `org-download-image-dir` to the directory of the current 
        buffer's file."
        (setq-local org-download-image-dir (concat (file-name-directory 
        (buffer-file-name)) "/images/" (file-name-base buffer-file-name) "/") )) 
    
    :hook
      ((org-mode . my-org-download-set-dir))

This creates the following structure for all inserted images:

├─images
| └── buffer-file-name
│     ├── First_heading_1
│     │   └── img1.png
│     └── Second_heading_2
│         └── img2.png
└── buffer-file-name.org

If you want to add a folder next to the file with the same name then remove "/images/" from the hook and replace with "/".

This will create the structure:

├─── buffer-file-name
│     ├── First_heading_1
│     │   └── img1.png
│     └── Second_heading_2
│         └── img2.png
└── buffer-file-name.org

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.