Can't get cursor position or selection when in properties

What I’m trying to do

I would like to create a new note from a template and set the name to be the text that is currently selected, even when that selected text is in a property field.

Things I have tried

I have a Templater file with the following code:

const nm = tp.file.selection();
await tp.file.create_new(
  await tp.app.vault.read(
    tp.file.find_tfile("atomic_template")
  ),
  nm
);
await tp.app.workspace.activeLeaf.openFile(
	tp.file.find_tfile(nm)
);

This works most of the time. However, it fails when the text I am highlighting is in a property field and the editor is in Live Preview mode. For example, I have a file with the following body:

---
ref: "[[Knowledge Management System]]"
---
Content

But when I highlight “Knowledge Management System” and trigger the insert of my template, I get an Untitled note.

I added some additional logging to my script and found:

  1. tp.app.workspace.getActiveFileView().editor.getSelection() is also null
  2. tp.app.workspace.getActiveFileView().editor.getCursor() returns a line and position that does not match the cursor’s actual position when I switch to Source mode

The trigger DOES work if I run it while toggled over to source mode.

I’m on Macos with Obsidian Version 1.12.7 (Installer 1.12.7).

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