Any way to replicate the behaviour of the Note Refactor plugin of Obsidian?

Greetings,
is there any way to replicate the behaviour of the Note Refactor plugin of Obsidian?

The plugin behaves this way:

  • I select a portion of text
  • I hit a key (or a combination of keys)
  • Instantly the plugin creates a new note with the first sentence as filename, and the other text as…text of the note
  • On the original note, the plugin leaves only the first sentence as a link to the new note

The latter point is optional for me, though much appreciated, but I wonder if I can replicate at least the other points on DT3.

I see there are some options to replicate the single steps, such as “Copy with Source Link”, but how about the full process?

1 Like

This is scriptable. Here’s a basic example with no error handling at all:


tell application id "DNtp"
	set theText to selected text of think window 1
	set theTitle to my getFirstSentence(theText)
	set selected text of think window 1 to "[[" & theTitle & "]]"
	create record with {name:theTitle, type:markdown, content:theText} in current group
end tell

on getFirstSentence(theString)
	set theDelimiters to (ASCII character 13) & (ASCII character 10) & ".:!?"
	repeat with theChar in theDelimiters
		set theOffset to offset of theChar in theString
		if theOffset is not 0 then set theString to (characters 1 thru theOffset of theString) as string
	end repeat
	return theString
end getFirstSentence

3 Likes

It works! Thank you.
I’ve set up a command on Better Touch Tools focused on DT3.

Alas I’m not fond of scripting, but I see I should dive deeper: I see I could do many interesting things on DT3.

Just one question: what if I wanted to have the title of the note and the item link (as per the option “Copy link in Markdown mode”, instead of the wikilink?
Is it possible?

You need to remember to be specific in your inquiries, especially regarding automation. The details matter. It’s apparent now you’re processing just Markdown documents into other documents, but you haven’t made that clear. If you change the first handler in @cgrunenberg’s script to this, it should work. However, you still need to keep the second handler intact…

tell application id "DNtp"
	if not (exists (content record)) then return
	if (type of (content record)) is markdown then
		set theText to selected text of think window 1
		set theTitle to my getFirstSentence(theText)
		set newRecord to (create record with {name:theTitle, type:markdown, content:theText} in current group)
		set selected text of think window 1 to "[" & theTitle & "…](" & reference URL of newRecord & ") "
	end if
end tell

This modifies the current Markdown document like this…