How to use an absolute path in getAbstractFileByPath

What I’m trying to do

Specify a file by absolute path, to be used with the tp.file.create_new function

I’m trying to accomplish creation of a file in a desired folder from a templater template

Things I have tried

I’m modifying a script from HOLROY which I found here: Inherit or pass on Metadata Values from Active File when creating New File - #3 by jibLord

My vault structure is:
Research
Research/Extracts
Test Files
Test Files/Test Extracts

The idea is for a note in Research, insert the Templater template, which runs javascript, which creates another note in Research/Extracts containing some of the same file properties as the original note. (this is for an academic linguist researcher)

The puzzle is in these lines:
// outputFolder3 is a subfolder of the root, not a subfolder of the source file’s folder. THIS WORKS
// Relative paths MUST name the root folder, and go from there, like this:
const outputFolder3 = app.vault.getAbstractFileByPath(“Research/Extracts”)

// outputFolder4 is specified by the absolute path
// this DOES NOT WORK. Dumps the file in the root instead
const outputFolder4 = app.vault.getAbstractFileByPath(“C:/Users/Max Fritzler/Documents/03 Matt’s Research Projects/Matts_Obsidian_Vault/3-Obsidian Vault/Research/Extracts”)

I should say I’m a moderately experienced amateur Python programmer, but have never touched Javascript.

So the question is, how to get an absolute path to work with this getAbstractFileByPath function?

thanks in advance

And here is the whole modified template for reference:
<%*
const dv = this.app.plugins.plugins[“dataview”].api

const curr = tp.config.target_file
const dvCurr = dv.page( curr.path )

// baseFolder is the folder where the file sits, e.g. Research/Test Files
// this works ok
const baseFolder = app.vault.getAbstractFileByPath(dvCurr.file.folder)

// outputFolder1 is a subfolder of the source file’s folder
// this works ok
const outputFolder1 = app.vault.getAbstractFileByPath(dvCurr.file.folder + “/Test Extracts”)

// outputFolder2 is the root folder, “Research”
// this works ok
const outputFolder2 = app.vault.getAbstractFileByPath(“.”)

// outputFolder3 is a subfolder of the root, not a subfolder of the source file’s folder
// Relative paths MUST name the root folder, and go from there, like this:
const outputFolder3 = app.vault.getAbstractFileByPath(“Research/Extracts”)

// outputFolder4 is specified by the absolute path
// this DOES NOT WORK. Dumps the file in the root instead
const outputFolder4 = app.vault.getAbstractFileByPath(“C:/Users/Max Fritzler/Documents/03 Matt’s Research Projects/Matts_Obsidian_Vault/3-Obsidian Vault/Research/Extracts”)
/**/

const newFile = “00 " + dvCurr.file.name + " extract.md”
/*
// Use this to prompt for a value which then can be used as part of a filename
\ const newFile = await tp.system.prompt(“New file name”)
*/
//
const content =
`—
tag: extract
participants: ${ tp.frontmatter[“participants”] }
comment: ${ tp.frontmatter[“comment”] }

Gloss:: ${ dvCurr.gloss }

`

console.log(“Content”, content)
console.log(curr)
console.log( dv.page(curr.path) )
console.log(baseFolder)
console.log(outputFolder1)
console.log(outputFolder2)
console.log(outputFolder3)
console.log(outputFolder4)
/*
*/

tR = [[${ newFile }]]
await tp.file.create_new(content, "00 base " + newFile, false, baseFolder)
await tp.file.create_new(content, "01 " + newFile, false, outputFolder1)
await tp.file.create_new(content, "02 " + newFile, false, outputFolder2)
await tp.file.create_new(content, "03 " + newFile, false, outputFolder3)
await tp.file.create_new(content, "04 " + newFile, false, outputFolder4)
/*
*/

_%>

I’m not quite sure what you’re trying to achieve here. The getAbstractFileByPath is used to get a file/folders relative to the vault location, so anything like "/", ".", or "Research" will always be relative to the vault.

Do you have something like that C:/Users/.../3-Obsidian Vault/Research/... and need to match that into your script somehow? Do you want to access files outside of the vault?

In the first case you’re able to get the base path of your vault using app.vault.adapter.basePath and can possibly do some string manipulation to match it.

But, I’m not entirely sure what you’re really asking for, so it’s hard to give a precise answer.

Thank you. I was indeed wanting to access files outside the vault, both by reading and writing them. Your explanation solves the problem for me. I should re-think the work flow and storage design so that everything is stored inside the vault. I can do that.

I was concerned that I was fundamentally misunderstanding how paths work in javascript. After I posted this, I eventually figured out how to paste and execute a line of code in the Obsidian console. Indeed, passing an absolute path string to getAbstractFileByPath returns null. At that point it seemed to me likely that getAbstractFileByPath was designed to handle only relative paths, and you have confirmed that for me.
Again,
Thanks

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