Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: tp.file.exists in Obsidian v1.0.0 always returns false for files; #878

Closed
jlharri opened this issue Oct 23, 2022 · 4 comments
Closed

Comments

@jlharri
Copy link
Contributor

jlharri commented Oct 23, 2022

Obsidian version 1.0.0, installer build v0.15.8
Templater version 0.11.13

I've tried multiple tests and tp.file.exists returns false for all files. It will return true for a folder.

Here's a test template I created and its output. Note that I've tried other hard-coded file names. I thought using tp.file.title might be the most obvious example of a failure.

<%*
console.log("--------");
console.log("Testing Templater tp.file.exists");
console.log("Obsidian version 1.0.0 - installer version v0.15.8");
console.log("Templater Version "+app.internalPlugins.app.plugins.manifests["templater-obsidian"].minAppVersion);
console.log("");

fn = tp.file.title;
console.log("tp.file.title <"+fn+"> exists? "+(await tp.file.exists(fn)));
fn = tp.file.folder(false);
console.log("tp.file.folder <"+fn+"> exists? "+(await tp.file.exists(fn)));
fn = "bogus file name";
console.log("Bogus file <"+fn+"> exists? "+(await tp.file.exists(fn)));
%>

Output copied from JS console:

Testing Templater tp.file.exists
Obsidian version 1.0.0 - installer version v0.15.8
Templater Version 0.11.13

tp.file.title <Test Templater Doc> exists? false
tp.file.folder <Test> exists? true
Bogus file <bogus file name> exists? false

btw, great plugin! It'd be nice to have the file.exists test part of tp.file.create_new as an optional parameter create_if_new.

Jerry

@AB1908
Copy link
Collaborator

AB1908 commented Oct 23, 2022

You need to await the exist() I think.

@jlharri
Copy link
Contributor Author

jlharri commented Oct 23, 2022

I do...it's in the code snippet above.

I figured it out. The documentation isn't clear about whether I can pass in just that name of a file, the name+extension, or the fully qualified path relative to the Vault. It's the last one. For example,

For a file located at Test/Templater Tests/Test Doc.md :

  • tp.file.exists("Test Doc") => false
  • tp.file.exists("Test Doc.md") => false
  • tp.file.exists("Test/Templater Tests/Test Doc.md") => true

So I put this into my template and it works:

fn = tp.file.folder(true)+"/"+tp.file.title+".md";
console.log("tp.file.title <"+fn+"> exists? "+(await tp.file.exists(fn)));

I hate that I have to hard-code the extension, but I realize most files in Obsidian are markdown. I also don't like hard-coding "/" in either since this may need to change for Obsidian running on Windows. There may be Obsidian utility functions that will abstract this.

I suggest updating the documentation to be clearer about what is needed.

@AB1908
Copy link
Collaborator

AB1908 commented Oct 23, 2022

Would you mind opening a PR to do that?

@jlharri
Copy link
Contributor Author

jlharri commented Oct 23, 2022

Of course...I should've thought of that myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants