Append to specific draft, by date

How can I use Shortcuts on iOS and KM on Mac to find a specific note in Drafts by title, and append to it? I can search by title, but then can’t manage to get the uuid for the note, which seems to be necessary for appending.

Here’s the use case: Every week I need to keep track of links, which I then use on Tuesday. I’ve been storing them in a note titled Links for 2020-02-25 (Or whatever the next Tuesday’s date is.) For another project, I have to do the same thing, but different links and for Wednesday.

So far, I’ve manually created the new notes each week, and added using the share sheet on iOS or copypasting on Mac, selecting the correct note each time. But I’d really rather just run one shortcut or KM macro each time — one for Tuesday and one for Wednesday — and have it save to the correct note without thinking about the date.

I’ve already got Shortcuts and KM creating the correct title string (using the next Tuesday’s/Wednesday’s date), and formatting the title/links/excerpts how I want them (in markdown). But I can’t figure out how to use that title string to find the right note and append to it (or create it if there isn’t such a note).

Any suggestions?

Thanks!

There’s not a way to “find by title” directly…I would tend to recommend, for consistency across platforms, to create Drafts action which you can can with the /runAction URL action.

That would have to be a scripted action which did a query to find if the draft existed, created it if not, and updated it’s content. Something like the Add to List example could get you started, which does similar with list titles.

You could do all this with Shortcuts, but not with KM outside of Drafts.

That’s great — thanks! I don’t mind doing it with a Drafts script action; as you say, it’ll make it more consistent. I’ll take a look at the action you linked to and see if I can adapt it.

So I modified the script in the action you linked to and came up with the below.

It works almost perfectly for me. If you run it on the contents of a draft, it asks which of three lists you want to add it to; the first figures out the next Tuesday date; the second, the next Wednesday; and the third is a kind of default list that isn’t dated.

If the script doesn’t find find a list with the right name (including the date), it’ll create it. Either way, it appends the draft content as a markdown bulleted item.

I use this on my phone with a shortcut that turns text and a link (eg from a news app article) into a Markdown link, and then (using the Drafts URL scheme) creates a new draft and runs the action. On my Mac, a Keyboard Maestro macro accomplishes the same thing.

Thanks again!

// See online documentation for examples
// https://docs.getdrafts.com/docs/actions/scripting

function getNextDoW(dayName, excludeToday = false, refDate = new Date()) {
    const dayOfWeek = ["su","mo","tu","we","th","fr","sa"].indexOf(dayName.slice(0,2).toLowerCase());
    if (dayOfWeek < 0) return;
    refDate.setHours(0,0,0,0);
    refDate.setDate(refDate.getDate() + !!excludeToday + (dayOfWeek + 7 - refDate.getDay() - !!excludeToday) % 7);
    return refDate;
}

// EDIT THIS
// setup categories available
const categories = ["Topic 1", "Topic 2", "Other"];

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

// grab text
const currentContent = draft.content.trim();

// 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;
   if (category == "Topic 1") {
      var day = "tuesday";
	}
	else if (category == "Topic 2") {
	   var day = "wednesday";
	}
	else {
	   var day = null;
	}
	// alert("day is " + day);
	if (day) {
		date = getNextDoW(day);
		yr = date.getFullYear();
		mo = date.getMonth()+1;
		dy = date.getDate();
		dfmt = " for " + yr + "-" + ("0" + mo).slice(-2) + "-" + ("0" + dy).slice(-2);
	}
	else {
		dfmt = "";
	}
	// alert("dfmt is " + dfmt);
	var noteName = category + " Links" + dfmt;
	// query for list drafts...
	var drafts = Draft.query(noteName, "inbox", [listTag]);
	// loop over found drafts looking for a matching list
	var d;
	for (var draft of drafts) {
		if (draft.content.startsWith("## " + noteName)) {
			d = draft;
		}
	}
	// if we didn't find the list, create it...
	if (!d) {
		d = Draft.create();
		d.content = "## " + noteName + "\n\n";
	}
	// tag and update content
	d.addTag(listTag);
	d.content = d.content + "- " + currentContent + "\n\n"
	d.update();
}
else { // user cancelled prompt
	context.cancel();
}