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)
/*
*/
_%>