Hey everyone!
I have decided to try and move my life from OneNote to Obsidian, my main use has been for journaling and I am tryingt to automate part of the process by creating a Journaling dashboard.
What I’m trying to do
I am using Templater and the buttons plugins.
I am trying to get something like this:
- Click the Button
- Create Template with name
<%tp.date.now("Do - MMMM")%>
- Move it to the designated folder
- If the folders dont exist, create them based on
Personal/Journal/Entries/Year/Month
- If a file exists with that name, delete the created file and open the existing file in the folder structure
Things I have tried
Below is where I am currently at! The file creates, and moves and creates folders.
I am currently stuck with switching to the existing document and deleting the other.
I am trying to avoid custom scripts in templater as I want it to work on iOS also.
I am not the best programmer but I am trying, any help would be appreciated!
Code in template:
const rootDir = `Personal/Journal/Entries/`;
const yearDir = `${rootDir}${tp.date.now("YYYY")}/`;
const monthDir = `${yearDir}${tp.date.now("MMMM")}/`;
const rootExists = this.app.vault.adapter.exists(rootDir, false);
const yearExists = this.app.vault.adapter.exists(yearDir, false);
const monthExists = this.app.vault.adapter.exists(monthDir, false);
Promise.all([rootExists, yearExists, monthExists]).then(
async ([rootExists, yearExists, monthExists]) => {
try {
if (!rootExists) {
await this.app.vault.createFolder(rootDir);
}
if (!yearExists) {
await this.app.vault.createFolder(yearDir);
}
if (!monthExists) {
await this.app.vault.createFolder(monthDir);
}
} catch (err) {
console.log(err);
}
}
);
try {
await tp.file.move(`${monthDir}${tp.file.title}`);
} catch (err) {
console.log(err);
const files = await app.vault.getMarkdownFiles();
const activeEntry = files.filter(
(file) => "/" + file.path === `${monthDir}${tp.file.title}.md`
)[0];
const thisFile = files.filter(
(file) => "/" + file.path === `${tp.file.title}.md`
)[0];
this.app.vault.on('delete', (toDelete) => {
this.app.workspace.activeLeaf.openFile(activeEntry);
});
await this.app.vault.delete(thisFile);
}