I’m trying to build a solution that puts the folder name in which a file is located in a property called “Main”.
I found the following script as a suggestion from another user, but it seems to not be working and I’ve spent already lot’s of time trying to fix it with no success.
What error are you getting?
I know that the last two lines work. I have it in several of my scripts.
For debugging, use the JavaScript console. See what you get on the line that extracts the path.
What do you mean “running”? This is a template code, and will be inserted/run when you call the template. It’ll not keep on running, and repeating itself.
Run it invoking insertion mode with Alt + E, then type the name of the template. Calling the template in create mode Alt+N will add nothing, since getActiveFile() works on the current note.
One more thing, since you are extracting two positions to the left of the array, you have to ensure that the note to be modified is under a subfolder, otherwise will return {}, an empty object. That can be addressed by switching your if(...) line with if (pathArray.length > 1) {subProject = pathArray.at(-2)} else {subProject = "/"}
But sill is not a satisfactory solution if you have nested subfolders, in which case - because of pathArray.at(-2)- the folder name will always be the immediate parent folder.