Help appending to current active draft

I found this extremely useful script that search a specific workspace for drafts, then pull each line tagged with a hashtag and append it to a new draft, along with the title of the draft where the line came from.

I have been trying to modify it to allow for the action to append to the current draft rather than creating a new draft, however, no success.

Was hopping that someone smart can help :smiley:

This is the script

// Get hashtagged tasks from all projects

// Global variables. 
// Set the "tag" to whatever hash tag you want to use

let tag = "#nextaction";
let date = Date.today().getWeek();
let n = "";

// find all drafts in the "Projects" workspace archive. Set your workspace and query accordingly (you might want drafts in the inbox, or all, or whatever)

let ws = Workspace.find("Projects");
let dList = ws.query("flagged");

// Loop through each draft

for (let z in dList)
{
    
    // Split drafts into lines

    let d = dList[z].content;
    let lines = d.split("\n");

    var t = "";
    var bool = false;
    var q = "";

    // Look for #nextaction tag in each line

    for (let line of lines)
    {
        // Grab the project name from the title of the Draft
        t = dList[z].processTemplate("[[title]]") + "\n\n";
        
        // If the line includes the #nextaction tag, set the bool to true and write the task to the q variable
        if (line.includes(tag))
        {
            q += line + " [link](" + dList[z].permalink + ")\n";
            bool = true;
        }
    }

    // If the bool is set to true, write the project name and all tasks to the n variable
    if (bool == true)
    {
        n+= t + q + "\n";
    }
}

let c = Draft.create();
c.content = "# Tasks for Week " + date + "\n\n" + n;

// Set whatever tag / flag values you want here for the draft

c.addTag("project");
c.isFlagged = false;
c.isArchived = false;
c.update();

Author @derekvan

Change these lines at the end of the script to something like this:

let c = draft.content
draft.content = c + "# Tasks for Week " + date + "\n\n" + n; //the "c+" part at the beginning of this line will put the original content of the current draft before the tasks. remove that if you don't want it.

// Set whatever tag / flag values you want here for the draft

draft.addTag("project");
draft.isFlagged = false;
draft.isArchived = false;
draft.update();

basically, the “draft” variable is the way of referencing the current Draft in the editor.

3 Likes

Ahh can’t thank you enough, the uses I can have for this script are immense.
Also, it works like a charm.

1 Like

Can someone upload this as an action? Seems pretty useful

If you think it might be useful, you could do this yourself. The only foundational requirement is a pro subscription, but that’s relatively inexpensive and if you are getting benefit from Drafts is almost guaranteed to be worth the investment. After that, editing the action with the instructions above should be trivial.

Thanks, got it, I’ll give it a shot for sure