Add To Drafts List - Reorder List of Lists

I use the “Add to Drafts Lists” a lot to add specific items to the various lists that I have set up in Drafts. When I hit this action, it generate a pop up displaying my “List of Lists” as you can see in the screen grab below. In this interface, how do I sort the lists alphabetically?

We would need to see the content of the action. There’s not an “Add to a Drafts List” action in the Directory that I’m immediately seeing – is this a modified version of the “Add to List” example? A custom action you wrote?

If you could post the script used in the action, I’m sure we could point out where to add the necessary sort function to sort the list of drafts prior to adding the buttons to the prompt.

Pretty sure it’s the same script as what you refer to above…I probably got name wrong.

Code below

// Add to List

// User values (editable)
const defaultBullet = "-"         // Alternatives: * or +
const defaultChecklist = "- [ ]"  // Alternatives: "* [ ]" or "+ [ ]"
const listTag = "lists"           // Alternatives: ¯\_(ツ)_/¯ (whatever you want it to be)

var selectDraft = function() {
	var drafts = Draft.query("", "all", [listTag]).reverse()  // Get all list drafts newest to oldest
	var titles = drafts.map(function(d) { return d.title })     // Get titles of all drafts
		
	var p = Prompt.create()
	p.title = "Select List"
	for (t of titles) {
		p.addButton(t)
	}
	const newListButton = "+ New List…"
	p.addButton(newListButton)
	var didSelect = p.show()
	
	if (didSelect) {
		if (p.buttonPressed == newListButton) {
			return newList()
		}
		else {
			return drafts[titles.indexOf(p.buttonPressed)]  // Return selected draft
		}
	}
	else {
		return false
	}
}

var newList = function() {
	// Prompt for title
	var p = Prompt.create()
	p.title = "New List"
	p.addTextField("title", "", "", { "placeholder": "Title", "autocapitalization": "sentences" })
	p.addButton("Create")
	var didCreate = p.show()
	
	if (didCreate) {
		// Create draft with given title
		var d = Draft.create()
		d.content = "# " + p.fieldValues["title"] + "\n"
		d.addTag(listTag)
		d.update()
		return d
	}
	else {
		return false
	}
}

var selectListType = function() {	
	// Prompt for list style
	p = Prompt.create()
	p.title = "Select List Type"
	p.addButton(defaultBullet + " Bulleted")
	p.addButton("1. Numbered")
	p.addButton(defaultChecklist + " Checklist")
	var didSelect = p.show()
	
	if (didSelect) {
		var b = p.buttonPressed.match(/^.+(?= [a-z]+$)/i)[0]  // Extract bullet from selection
		if (b == "1.") { b = "0." }                           // Set up for addBullet() function
		return b
	}
	else {
		return false
	}
}

var addBullet = function(text, index) {
	var indent = text.match(/^[\t ]*/)    // Get leading whitespace
	var n = parseFloat(bullet)            // Get numeral if numbered list
	if (n || bullet == "0.") {            // Include condition where new numbered list created
		n += index + 1                     // Iterate numeral by one
		return indent + n + ". " + text.trim()
	}
	else {
		return indent + bullet.replace("[x]","[ ]") + " " + text.trim()  // Replace checked box with empty box
	}
}

const bulletRegex = /^([\t ]*)([-*+]( \[( |x)\])?|\d+\.)/gm
var bullet = ""
var d = selectDraft()

if (d) {
	var matches = d.content.match(bulletRegex)
	if (matches) {  // No bullets found
		bullet = matches.reverse()[0].trim()
	}
	else {
		bullet = selectListType()
	}
	console.log(bullet)
	if (bullet) {
		var text = draft.content.trim()         // Remove white space on either end
		text = text.replace(bulletRegex,"$1")   // Strip existing bullets but leave indentation
		var lines = text.split("\n")
		lines = lines.map(addBullet)
		d.content += "\n" + lines.join("\n")    // Join and append to draft
		d.update()
		editor.load(d)  // Open list
	}
	else {
		context.cancel()
	}
}
else {
	context.cancel()
}

This is the line that fetches the drafts:

var drafts = Draft.query("", "all", [listTag]).reverse()

If you change that line to the following, they will come back sorted by the text of the draft:

var drafts = Draft.query("", "all", [listTag], [], "name")

That did the trick! Thanks!