Excluding the [[title]] from scripting action

Morning team

I am using a modified action from the Directory to add Projects to my GTD system. For the import, the script adds a # to the end of each line to delineate tasks. I want to be able to use the [[title]] first line as the Project Name. It does not need the # on the end.

This is the script:

// Add the # character at the end of each line
var lnText = draft.content;
var lines = lnText.split('\n');
var newText = lines.join("#\n").replace(/\|/g, '\n\t');
draft.content = newText;

How do I modify the script to exclude the first line of the Draft please? I did look at the reference, but this is my first go at modifying a script!

Any assistance is very much appreciated. Thank you!

Generally, I recommend using the template engine. In script, that would be:

let body = draft.processTemplate("[[body]]")

Then just use the body variable later in your script. There’s no need to update the content of the draft to reflect the change. See related docs for details.

If you just wanting to use that value in a later action step that uses templates, there’s no need to script it, just use [[body]] in that step, and maybe [[display_title]] to get your stripped title value. More details on using templates.

Hope this helps!

Thanks Greg, I have gotten to here now:

// Add the # character at the end of each line
let body = draft.processTemplate("[[body]]")
var lines = body.split('\n');
var newText = lines.join("#\n").replace(/\|/g, '\n\t');

For testing, the next step is to create a new Draft (rather than email). The Output is:

Project Title

The input Draft is:

Project Title
Task One
Task Two
Task Three

So based on what I have, my questions are:

  1. Is this script adding a # to the end of each. “Task” line?
  2. Is the updated text saved into the newText variable?
  3. Why is the newtext variable not loading in the created Draft?

Thanks again!

For Q1 and what you have posted, no. There would need to be a blank line after the final task because join is only adding between lines. I.e. the last line would be missing the octothorpe.

If you can’t guarantee that, you could explicitly append one onto the last line or use another regular expression in a replace instead of the join. The latter approach you could even use to match only task lines if you had a mix and a particular format to your original tasks.

For Q2, yes.

For Q3, there is nothing shown in the script above for creating a new draft. If that is the entirety of your script, then you are missing the creation of a new draft and you can build it based on the first example on this page:

If you are not missing that creation piece, then you need to post those details so we can see what is going on.

Awesome, thanks for your help @sylumer!

I can certainly just leave a blank line at the end of the file, but I think the syntax for the import will work either way.

Ultimately I don’t want to create a new draft, I want to do the transforms in the script and then email the result. I had this set up using the Email step and just inserted ‘[[newText]]’ as the variable, but it doesn’t seem to work. This is the email action:

When I run this, it gets the Subject correct, but the body ends up with just ‘[[newText]]’ in the body.

I am not tied to using a step though, just as happy to script that also as it goes FROM/TO the same emails every time.

Thanks again!


I figured it out! Needed to add an additional line to the script:

ADDED: draft.setTemplateTag("taskList", newText);

// Add the # character at the end of each line
let body = draft.processTemplate("[[body]]")
var lines = body.split('\n');
var newText = lines.join("#\n").replace(/\|/g, '\n\t');
draft.setTemplateTag("taskList", newText);

Once I did that, works like a treat!

As an additional question though, if I use the Mail scripting rather than the step to set up the email, how do include the TemplateTag in these:

mail.subject = "«TemplateTag» other text";
mail.body = "«TemplateTag»";

If I can get that resolved, I can make it a one-step action.

1 Like

Use the same processTemplate function you used to get the body (:

mail.subject = draft.processTemplate("[[display_title]] other text")
mail.body = draft.processTemplate("[[newText]]")

Legend! Thanks Greg and thanks @sylumer! You guys are amazing, your help has been very much appreciated.

1 Like