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.
// 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
[[newText]]
The input Draft is:
Project Title
Task One
Task Two
Task Three
So based on what I have, my questions are:
Is this script adding a # to the end of each. āTaskā line?
Is the updated text saved into the newText variable?
Why is the newtext variable not loading in the created Draft?
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.
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:
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.