Add metadata on page using scripting

There are indeed multiple references with various context to be used slightly different depending on whether you’re using a DQL query or dataviewjs query:

  • a link – Which has the properties of path, display, etc
  • a page reference - Which is used to access the properties of a given page (and related file information)
  • a TFile reference – Which is more of an internal file reference

In DQL you normally use the link and page references, and they sometimes even coexists to make it easier for the end user. Within dataviewjs, they don’t coexists and you need to be careful whichever variant you’re using. You can however access both variants with proper code. Lastly the TFile can be found as a return option from various calls, and I’ve so far used the following method (given a file path to the page I want to access):

// Using Templater's tp.file.find_tfile
const tp = app.plugins.plugins['templater-obsidian'].templater.current_functions_object
const tFile = await tp.file.find_tfile(file.path)

// Using _Dataview_ functions
const tFile =
  app.metadataCache
     .getFirstLinkpathDest(dv.io.normalize(file.path, "")

See Open a file in callback - #2 by holroy for slightly more information on these two methods. In the code above file refers to some file link, like in a link context, so that the .path is available providing the file name (with path) of that link.

Hopefully this explains some more about the TFile thingy, and how to access is from dataviewjs (or similar). The app.workspace.getActiveFile is another way, with its caveats, to get to a relevant TFile in your context.