Logging In Templater

Things I have tried

I have just watched the video tutorial for this and entered the following

const file = tp.file.find_tfile(tp.date.now("DD-MM-Y dddd"))
if (file) {
	const loggedItem = await tp.system.prompt("What's Up?")
	const time = tp.date.now("HH:mm")
	const content = await app.vault.read(file).split("\n")
	const index = content.indexOf("### Log")
	content.splice(index + 1, , `- ${time} - ${loggedItem}`)
	await app.vault.modify(file, content.join("\n"))
} else {
new Notification("No Daily Note Found!")
}
-%>

What I’m trying to do

However I cant get this work. Am i missing something?

2 Likes

It would be helpful to have a link to the video tutorial. Thanks!

The video is this one from channel Obsidian: Templater 101 with Sam Morrison

Multiple persons (OP and I included) get the following error:

plugin:templater-obsidian:61 Templater Error: Template parsing error, aborting. 
 app.vault.read(...).split is not a function
log_error @ plugin:templater-obsidian:61
errorWrapper @ plugin:templater-obsidian:81
await in errorWrapper (async)
append_template_to_active_file @ plugin:templater-obsidian:3700
callback @ plugin:templater-obsidian:3978
BB @ app.js:1
e.executeCommand @ app.js:1
e.onTrigger @ app.js:1
e.handleKey @ app.js:1
e.handleKey @ app.js:1
t.handleKey @ app.js:1
e.onKeyEvent @ app
.js:1

From a general programming perspective I’ve got two suggestion regarding this issue.

Firstly you’re calling the split function on an async function, so I’m not sure how secure that is. I think it would be better to do the await first, and then do the split afterwards, if there is content in the file.

Secondly, does this happen with all files or just with newly created files (or even non-existing files)? You might need some checks to verify the file exist, and have contents before you try to split it into parts.

Either way I would split the line with the read, debug the output from the read so that you can see when and why the split fails.

Hi there and thanks for your reply. In the meantime i have found the solution.
I have resolved it by changing the code as follows:

Before:
const content = await app.vault.read(file).split(“\n”)

After:
const content = (await app.vault.read(file)).split(“\n”)

In essence that is doing what I commented upon, namely doing the await on the read, and not the split. You just don’t store the result in an intermediate variable (nor check it for it being empty/undefined).

Good for you that you found a solution, though! :slight_smile:

1 Like

Yes, thanks. We had the same idea.

Here’s my code btw: pastebin

The script has now all basic functionalities. Works perfectly well. It creates an entry in the Day Planner note, puts it in the right place, and does some checks ( correct format, no duplicates …)

I would like to include the Obsidian ICS plugin by calling the command directly with the Obsidian API. But this plugin only works if the active note is named “YYYY-MM-DD”. I will see if i can modify the plugin or find some other approach.

I would like to grab my Google Calendar in the Day Planner. With my script i could then add events on the fly.

I’m chatty today…