Templater script to create a new note for annotating selected pdf

I’ve been wanting to read more non-fiction and I like taking notes while I do it so I wrote a script to create a new note from a selected pdf

the script itself uses some Templater functions in addition to Obsidian ones (and vanilla JS) as well as Templater for the literal template. I’m using QuickAdd to trigger running the script as a macro (because I already use it for other things and then I don’t have to make a note just to trigger it via Templater). it doesn’t actually use Annotator but the notes it generates assumes that’s what you’re using to annotate your pdfs

let me know if you find it useful or you have any questions!

const splitFilename = (filename) => {
  const split = filename.split('-')
  const title = split[0].replaceAll('_', ' ')
  const author = split[1].replaceAll('_', ' ')
  return { title, author }
}

async function createNoteFromAnnotationTarget ({ app, obsidian, quickAddApi, variables  }) {
  const tp = app.plugins.plugins['templater-obsidian'].templater.current_functions_object
  // get all the files that are are in the non-fiction folder
  //  the non-fiction folder is where I put all the pdfs I want take notes on
  const files = [...app.vault.getFolderByPath('assets/non-fiction').children]

  let pdfFileData
  // format the file title for the prompt
  const pdfNames = files.map(file => {
    // pull out the title and author separately for later
    pdfFileData = splitFilename(file.name)
    return `${pdfFileData.title} - ${pdfFileData.author}`
  })
  // get the TFile of the pdf
  const pdfFile = await tp.system.suggester(pdfNames, files)
  // if the user chooses a file then make a note about it
  if (pdfFile) {
    // 'notes_non-fiction' is the name of the template note
    await tp.file.create_new(tp.file.find_tfile('notes_non-fiction'), `notes - ${pdfFileData.title} - ${pdfFileData.author}`)
    .then((note) => {
      // when the note is created add the pdf and author to the frontmatter
      app.fileManager.processFrontMatter(note,
        (fm) => {
          fm['annotation-target'] = pdfFile.name
          fm.author = pdfFileData.author
        }
      )
      // open a new leaf in a split window and open the new note in it
      app.workspace.getLeaf('split').openFile(note)
    })
    .catch((e) => {
      // really good error handling I know
      console.log('oh nooo ', e)
    })
  }
}