Search draft and replace prefix

Can’t believe I’m asking for this basic-level help knowing there’s a lot of documentation concerning prefixes and RegEx expressions, but I can’t figure out how to create a script for:

Converting any line beginning with “- [ ]” to “- [x]”

Example:
-[ ] This is a task

(becomes after running the script)

-[x] This is a task

Thank you.

A part of the script I currently use do this, using .replace() rather than a regex, assuming you already know how to target the lines you want to update (e.g. editor.getSelectedLineRange() followed by editor.getTextInRange())

if (lineText.includes("- [ ]")) { lineText = lineText.replace("- [ ] ","- [x] ") if (lineText.includes("@inprogress")) { lineText = lineText.replace(" @inprogress","") }

You could do it with a regex that targets the start of the line, but since I don’t anticipate this particular series of characters being used in any other context, .replace() suits my purposes.

I also use an @inprogress tag for tasks I’m currently doing; the script fragment above removes the tag when marking the task done.

Thank you for assisting with the scripting.

Unfortunately, no. I don’t have the skills to set up the intro you referenced:

editor.getSelectedLineRange() followed by editor.getTextInRange())

But I can look it up. In short, I need the script to examine the entire draft and act on any lines in the draft that have the specific prefix.

Try:

var r = editor.getSelectedLineRange()
var sel = editor.getTextInRange(r[0],r[1]).replace(/\n$/, '')

var sel = sel.split("\n")
var newsel = ""

sel.forEach(function(lineText){

 if (lineText.includes("\t")) {
  var tabCount = (lineText.match(/\t/g)||[]).length
 }
 
 if (lineText.includes("- [ ]")) {
  lineText = lineText.replace("- [ ] ","- [x] ")
 
 }
 newsel += lineText + "\n"

})

editor.setTextInRange(r[0],r[1],newsel)
draft.update()

editor.setSelectedRange(r[0]+newsel.length-1,0)

Thank you for creating this.

Oddly, it had no effect. No change to the corresponding prefixes.

Strange. Worked for me when I tested it.

I’m assuming that you’re running this action with the cursor on a line with the task prefix, or a selection of lines with the task prefix?

Ahhhh. In that use case, yes. After I selected many rows, it worked as you expected. Thanks!

In my desired use case, selecting lines of text or specifically placing the cursor wouldn’t be necessary. Instead the script would scan the entire draft and action on those lines with the correct prefix.

I appreciate all the help and am happy to “buy coffee” for your time.

Should work if you select one row, many rows and/or rows with indented tasks (allowing for different levels of indents).

That said, bearing in mind your use case, you could probably do something as simple as:

var text = editor.getText()
text = text.replaceAll("- [ ]","- [x]")

editor.setText(text)
draft.update()

:wink:

@agiletortoise does draft.update() trigger draft.saveVersion(), or would I need to add that here if I wanted a fallback?

1 Like

And it was that simple. That did it! Thank you.

1 Like

If you are using editor methods, no need to call update on the draft, just let the editor manage saving and versioning.

Those methods all participate in the editor’s undo stack as well, so the change could be undone if not desired.

1 Like