一种在创建未命名文件并重命名之后自动调用templater模板的方法

我的痛点:templater设置里可以为某个文件夹的新建文件自动添加模板,但是如果创建的是未命名的文件,就会变成下面这样。然后就要删除文本再重新手动调用模板,太麻烦了o(´^`)o,我肯定选择重命名后再自动插入模板。

<%*
let title = tp.file.title
let path = tp.file.path(1)
if (title.startsWith('未命名')) {
    let e;
    e = app.vault.on('rename', async(file, oldPath)=>{
        app.vault.offref(e)
	    if(oldPath==path) await app.commands.executeCommandById('templater-obsidian:000-HomePage/Data/Templater/新建笔记.md')
    })
    setTimeout(()=>{app.vault.offref(e)}, 10000)
} else tR += await tp.file.include(`[[06-卡片]]`)
-%>

bandicam 2023-07-21 20-32-51-340

使用方法

把目标文件夹的自动模板换成上面的模板

在设置里为这个模板创建一个命令

然后将代码里的路径换一下(从设置里复制出来就行了)

app.commands.executeCommandById('templater-obsidian:自己的路径')

这里面的名字换成想要插入的模板就可以了

tp.file.include(`[[模板]]`)
4 个赞