Rename File Script as Created or call external script

Hello everyone,

I’m not sure if anyone has done anything like this yet.
I know it be pretty easy to do in .Net but im not sure about JavaScript.

My idea it this:
Whenever I drop an image into a note, say .png or .jpg
I would like to rename this picture splitting on “_” for example
Code_C_Topic

My image would be: Code_C_Topic00 and itll increment by 1 each time which can be done with an exists and a counter.

If this possible with Javascript?
Else is there a way I can run an external script through some command in obsidian?

Thank you

I’ve never used any of them myself, but you’ll find several promising community plugins if you search with “attachment” or something, like Attachment Name Formatting.

(And even if there isn’t a nice plugin that meets your demand, almost everything is possible if you make your own plugin using TypeScript. )

Else is there a way I can run an external script through some command in obsidian?

You can use QuickAdd user scripts for this purpose.
In some cases, you don’t need to create a plugin by yourself, and a QuickAdd macro suffice.

For example, if you set something like the following script to run on the plugin load, any non-markdown file will be renamed right after its creation following the rule you define.

module.exports = (params) => {
    const {app, obsidian} = params;

    app.workspace.onLayoutReady(() => {
        app.plugins.plugins.quickadd.registerEvent(
            app.vault.on('create', (file) => {
                if (file instanceof obsidian.TFile && file.extension != 'md') {
                    app.fileManager.renameFile(file, <NAME_YOU_LIKE>)
                }
            })
        )
    });
}

Will this work for when an image is pasted into a note?
That is actually wnat I am trying to do with this. When I paste an image into a note it will auto name it and increment the number at the end

Update: I replaced app.vault.rename with app.fileManager.renameFile because the latter auto-updates links when renaming files.

Will this work for when an image is pasted into a note?

It will. If you want to only target PNG files, for example, then replace file.extension != 'md' with file.extension == 'png'.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.