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:
- Another JavaScript function in the same script step of the currently being run action.
- Another script step in the currently being run action.
- 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.