Adding selection to new/existing list

I am trying to do the following:

In a draft, highlight a section and send it to a new/existing list.

The two actions which do something similar are at:

Add to list plus | Drafts Action Directory, and,
Add to list | Drafts Action Directory

Both of these take the whole contents of the draft and append it to a new/existing user selectable list. The whole contents. I don’t want to do that. I just want a selection from the document appended/prepended to a new/existing list with the Markdown task markup.

Is that possible? How?

macosxguru

Both actions use draft.content to get the draft content.

Change this to editor.getSelectedText() to use selected text instead.

1 Like

Thank you. Will try that out.

macosxguru

This is an example of what I called a fragment in this post.

1 Like

Yes. I am clueless when it comes to any kind of programming. So, I am taking baby steps.

But you are right:

That example is just a "how to read the draft text, do something to it and put it back.

macosxguru

1 Like

Right. And the notion / idea / whimsy / is to make such baby steps easier to take, while enabling you to do real work.

Okay. That worked. Thank you.

Now a little more complicated a problem.

This is the script:

/*
  Ask for a category, and append to a tagged list for that category
*/

// EDIT THIS
// setup categories available
const categories = ["Blog Ideas", "Books", "Computer", "Movies", "Shop"];

// EDIT TAG
// tag to assign to list drafts
const listTag = "listitems";

// grab text
const currentContent = editor.getSelectedText();

// prompt to select a category
var p = Prompt.create();
p.title = "Select list";
for (var cat of categories) {
	p.addButton(cat);
}

if (p.show()) { // user made a selection
	var category = p.buttonPressed;
	// query for list drafts...
	var drafts = Draft.query(category, "inbox", [listTag]);
	// loop over found drafts looking for a matching list
	var d;
	for (var draft of drafts) {
		if (draft.content.startsWith("## " + category)) {
			d = draft;
		}
	}
	// if we didn't find the list, create it...
	if (!d) {
		d = Draft.create();
		d.content = "## " + category + "\n\n";
	}
	// tag and update content
	d.addTag(listTag);
	d.content = d.content + "- [ ] " + currentContent + "\n"
	d.update();
}
else { // user cancelled prompt
	context.cancel();
}

This considers only one line. If you have multiple lines, it treats it as one line with \n in between. Doesn’t add the checkbox to multiple lines, only the first line. How does one make it aware of a series of items to add to a list?

macosxguru

Please explain what a list is. I can think of two meanings.

  • A bulleted list in a single draft.
  • A workspace.

My guess is you mean the latter - but I’m not sure.

I mean a list of items/paragraphs. They are not bulleted. They are just lines of text.

macosxguru

1 Like

Look up “arrays in JavaScript”. Arrays are ways of breaking something up into multiple elements and being able to reference and process them individually. If you can split your text into an array of elements with each element being a line, then you simply work through (iterate over) all of the elements in the array and process them one at a time.

How do you split a string of text? Look up “JavaScript split method”. Use the newline character as your split character and you’ll have your array.

1 Like

Every time I guess wrong that justifies my asking for clarification. :slight_smile:

Thank you. More studying in my future. :slight_smile:

Thanks for pointing me in the right direction.

macosxguru

Thank you. :slight_smile:

macosxguru