Using templater to automatically remove tabs from files imported by readwise so that note refactor works correctly - stuck

Things I have tried

My current setup involves this templater code in the readwise highlights section:

and this script in the scripts folder of my Obsidian vault:

CleanShot 2022-12-30 at 13.37.00@2x

In theory… this should work.

If I the file after I app.vault.modify it to remove the tabs… I can see that the tabs are removed (see bottom left part of each console.log).

(Note: I don’t think the TypeError from Meta Edit is relevant here.)

Yet, when I look at the file after templater has ran (and I know it has because the templater code is gone from the note), I see that the tabs are still there:

So, I’m stuck. Any ideas?

What I’m trying to do

Hey everyone. I noticed that the Note Refactor plugin doesn’t work when there are tabs. Demo:

CleanShot 2022-12-30 at 13.31.47

So, I want to remove all tabs in this note.

Except… this note was created automatically by readwise as an import.

I don’t want to have to do this for every note readwise imports.

Hence, I want to leverage templater to do it automatically when readwise imports the note, for each note readwise imports.

I’ve not seen too much on the rest of your script, but I would rather use “\t” to match tabs, then an actual tab inserted into the string.

And shouldn’t you await the modifying of the file also? And check for error result of the various calls (if they give them).

  • re: using \t for tabs, I was actually using 4 spaces as the string. I tried using \t (i.e. t.replaceAll("\t", "");) and it didn’t do anything:

CleanShot 2022-12-30 at 15.27.52@2x

  • re: await before file modification, that makes sense. Will try that. I haven’t seen many examples use await - I wonder why they don’t? Anyway, here’s a rerun with await and using replaceAll(". ", "") as I had before:

CleanShot 2022-12-30 at 15.32.26@2x

Same thing as before: it shows that the tabs were replaced in the console.log, but the note itself in obsidian shows no tab replacement:

  • re: error result. There are none related to this specifically, that I can tell. The TypeError after the console.log I believe is from Meta Edit and is unrelated.

Does that mean you’re running the script from within the file itself?

If that’s the case, that could be your issue as well. Then that would need to be kept in memory, whilst you then reread and change the file. If nothing else that does sound like you’ve got some unhealthy race conditions going on.

A few hours back I prepared a script to change the frontmatter of multiple files, and I’m wondering whether something similar could be used in your case. But I would never run that (or similar) script from within the note itself. To me that sounds like sawing off the branch you’re sitting on.

1 Like

It’s not obvious to me where it’s running from. I assumed it wasn’t running from within the file, rather from within Obsidian.

Oooh, I would be interested in this script!

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