I can’t make the “tp.file.create_new” work as expected, the following is my code.
After running the code, the file is always created at the default folder of my obsidian, not the ref_path that I specified.
Any help will be appreciated.
let title = await tp.system.prompt("Input Note Name", "test", false)
console.log('file name:', title);
let project_name = tp.config.active_file.basename
console.log('project name:', project_name);
let ref_path = `4-Project/Project-Reference/${project_name}/`;
let exists = await tp.file.exists(ref_path);
if (!exists) {
console.log('Creating path:', ref_path);
await this.app.vault.createFolder(ref_path);
}
else
{
console.log('exist path:', ref_path);
}
let tFolder = app.vault.getAbstractFileByPath(`${ref_path}`);
await tp.file.create_new(tp.file.find_tfile("tp-project-reference"), title, false, tFolder);
iam not a javascript guy, but i think you have to declare file after you create new folder? let file = app.workspace.getActiveFile(); before last line with await tp.file.create_new(...)
or you can create note and then move it to the right folder?
Why do you think I shoud getActiveFile?
I am also not a javascript guy, the “tFolder” is the target folder that I want to create file in, why should I declare the active file?
My intention is that, while I am editing the project plan, and then I found I need a new page to fill in the project reference, so I will need to click a button to create project reference for current editting project plan.
Thanks.
In JavaScript, I’m quite a beginner, and I must admit that I copied that part from my script without a full understanding of the syntax. It’s possible that it shouldn’t be that way, and my previous response was more of a theoretical explanation than an exact code example. I apologize if this caused any confusion. I’m not a native English speaker.
I’ve been using Obsidian for only 10 days, so I’m still learning, but I enjoy tackling challenges, which is why I responded to your question. A little brainstorming doesn’t hurt