Append to current draft

I borrowed an action from the library and rejiggered it to append a link in an existing draft note to the active note. It works, but I want the result to remain as the active note, so I can pass it to another action.

Here is my current script:

let f = () => {
// select an existing draft
let d = Draft.find(“01AB846B-FA59-4D98-91B2-E7F52DBE3C63”);
if (!d) {
return false;
}

// append current draft
draft.content = draft.content + " " + d.content;
draft.update();
return true;	

}

if (!f()) {
context.cancel();
}

What do I need to do to bring the revised note back as the active note?

Thanks.

Your action looks to me like it adds the content of another draft to the current draft, which is not what you describe. Can you confirm what you wish to do with an action, including if a draft is not found, etc?

Please also remember to put your script in a code block, denoted by placing it between a pair of triple back ticks (```).

Thanks, I was not clear. I want to append an existing draft to the active draft.

I have not yet decided what I want to do if there is no draft to append.

Eventually I may want to offer a few choices to append to the original, but that is not decided.

Right now I want the original to end up as the active draft so I can pass it to another script.

I have hit a dead end, and I think I should approach it a completely different way, but I am at a loss as to what that would be. A template possibly?

Added corrected script.
‘’’
let f = () => {
// select an existing draft
let d = Draft.find(“01AB846B-FA59-4D98-91B2-E7F52DBE3C63”);
if (!d) {
return false;
}

// append current draft
draft.content = draft.content + " " + d.content;
draft.update();
return true;	

}

if (!f()) {
context.cancel();
}
‘’’

to load a draft you can use editor.load() (scripting reference)
In your case try to add the following snippet:

…
draft.update()
editor.load(d)
return true
…
1 Like

Okay, that matches what the action was doing, so that clarifies things there. Specifically you are adding the content of a particular, pre-existing draft (presumably what you subsequently refer to as “original”) to the current draft loaded in the editor.

Based on that, your initial script can be simplified further through using append and maybe simplifying some of the if statements? Here is a potential revision:

let f = () => {
	// select an existing draft
	let d = Draft.find("01AB846B-FA59-4D98-91B2-E7F52DBE3C63");
	if (!d) return false;
	draft.append(d.content, " ");
	draft.update();
	return true;
}

if (!f()) context.cancel();

Also please note you tried to use single quotes rather than backticks on your repost of the code on the forum which is why it still did not work, as the forum truns quotes into smart quotes, loses any indentation, etc.

Your original code cancels the context, so perhaps just a message (we can do that through fail, which seems more appropriate) and then simplifying the script?

let d = Draft.find("01AB846B-FA59-4D98-91B2-E7F52DBE3C63");
if (!d) {
	let strErrMsg = "Specified draft to append not found.";
	app.displayErrorMessage(strErrMsg);
	context.cancel(strErrMsg);
}
else {
	draft.append(d.content, " ");
	draft.update();
}

In the interim, you could simply pop-up the standard Drafts draft search box and grab the result or deal with a cancellation from that; until such time as you are ready to constrain your results to a known subset of drafts.

let d = Draft.find("01AB846B-FA59-4D98-91B2-E7F52DBE3C63");
if (!d) d = app.selectDraft();
if (typeof d !== "undefined") {
	draft.append(d.content, " ");
	draft.update();
}

This is where we get to the response from @FlohGro, specifically the editor.load(d) line, but is also where we are again back in the realms of requiring more clarity.

That line of instruction will load the “original” draft into the editor. That line will work if you are going to manually run another action on the currently active draft subsequently, or on an action that has it loaded into the editor. BUT, you indicate that you are going to “pass it to another script”. That is non-specific enough to cover a variety of activities. “another script” could refer to any of the following and potentially a few other more complex options and variations too:

  1. Another JavaScript function in the same script step of the currently being run action.
  2. Another script step in the currently being run action.
  3. Another script step in another action that you manually trigger.

In addition, you can run an action on a different draft using the app.queueAction() function, which accepts a draft as a parameter, but that would not require the draft to be loaded into the editor.

Also keep in mind that the draft and editor objects are not equivalent. What do your subsequent scripts work on? What is currently loaded in the editor, a draft object that is passed to it, or the ‘current draft’ referenced by draft, which would be the draft on which the action was originally triggered.

It may well be that what you are after is something like:

let d = Draft.find("01AB846B-FA59-4D98-91B2-E7F52DBE3C63");
if (!d) d = app.selectDraft();
if (typeof d !== "undefined") {
	draft.append(d.content, " ");
	draft.update();
	editor.load(d);
	editor.activate();
}

But, it is at least equally as likely that it might be something else that you require. More specifics and more detail as what is required gets you clsoer to a final answer faster.

Hopefully, that helps clarify a few things to you, as well as helping you think about what it is you are attempting to do overall and the sorts of things you must consider.

1 Like

First, thanks for all the information. I really appreciate your helpfulness, and it will take me awhile to absorb it all.

Second, sorry for the single quotes. I was using the keyboard on the iPad screen, and I could not (still can’t) find the right key. I know what you are referring to, but I don’t find it anywhere on this keyboard.

third, I think I have managed to make this too complicated. Simply put, I want to insert text at the end of the current active draft (is that the right terminology?) before sending the active draft to another action.

I think “Insert Text” will meet my needs. But eventually I will want to offer several choices of text, one of which will have to be selected by me before continuing. I have created these kinds of choices before, but I am not fluent in it, and I will have to remember how to do it.

To explain what I am doing:

I use FlohGro’s action “Prepend to Craft Note as todo item.” But to use its full power of putting tasks into one of several documents, I need to add a markdown link at the end that will reference a page in Craft. That way, all tasks can be found in one of 2-4 pages instead of in one of possibly 20 documents. Currently I use TextExpander to add one markdown link manually.

I want to automate it, and I would eventually like to offer 3-4 choices.

The way I was approaching this was perfectly good, but using “Insert Text” is simpler. I just need to make sure the cursor is actually at the end of the line, which it usually is, and I need to remember how to offer a few choices.

Thanks to both of you for all your help, When I get home and can think more, I will absorb it and decide if that is better than Insert Text or not.

Press and hold the single quote to reveal more options.

If you are processing the draft, and want to continue processing the same draft, then you would not have to do anything. Unless you specify to start jumping around, or archiving/trashing drafts, Drafts will stay put.

Just to note it might not be necessary to alter the original draft. Perhaps this new text is something you actually want in the draft, but if it is just meta-data you are adding for the benefit of whatever other app or service you are targeting, you might just have the action include that text via templates, and not actually insert it in the original text.