Hi! I’m trying to run a script based on a draft languageGrammar. I want to achieve if draft languageGrammar is Markdown toggle checklist, if languageGrammar is Taskpaper, toggle done date in Taskpaper format.
So far, I was able to get languageGrammar for a draft and then run the script. What happens is that no matter if draft set to Markdown or Taskpaper, script runs only the Taskpaper part of script.
Script:
let dLG = draft.languageGrammar;
if (dLG = "Taskpaper") {
// Mark current task or selected tasks as done today.
// Set the done marker text with today's date.
var today = new Date();
var yr = today.getFullYear().toString();
var mo = today.getMonth() + 1;
mo = mo.toString().padStart(2, '0');
var da = today.getDate()
da = da.toString().padStart(2, '0');
var marker = ' @done(' + da + '-' + mo + '-' + yr + ')';
// Get all the currently selected lines.
var lines = editor.getSelectedLineRange();
var sel = editor.getTextInRange(lines[0], lines[1]).replace(/\n$/, '');
// Loop through the lines, adding the done marker to tasks only.
var selArray = sel.split('\n');
var numLines = selArray.length;
for (var i=0; i<numLines; i++) {
if (selArray[i].startsWith('-')) {
selArray[i] += marker;
}
}
//Replace the selected lines with the marked lines.
var doneText = selArray.join('\n');
var doneLength = doneText.length;
editor.setTextInRange(lines[0], lines[1], doneText + '\n');
editor.setSelectedRange(lines[0] + doneLength, 0);
}
else if (dLG = "Markdown") {
// Toggle tasks marks on selected lines
const off = "[ ]";
const on = "[x]";
// grab state
let [lnStart, lnLen] = editor.getSelectedLineRange();
let lnText = editor.getTextInRange(lnStart, lnLen);
let [selStart, selLen] = editor.getSelectedRange();
// check if any incomplete tasks are in selection
if (lnText.includes(off)) {
lnText = lnText.replaceAll(off, on);
}
else { // no incomplete tasks, mark undone
lnText = lnText.replaceAll(on, off);
}
editor.setTextInRange(lnStart, lnLen, lnText);
}
Aha, I did it actually yesterday evening, but gave up because then would Markdown part be execute but not Taskpaper… Now all works. I just needed to change “Taskpaper” to “TaskPaper”. Thank you as always @sylumer
In case this helps you understand the differences a little more, early on when I was learning to write code, I found it useful to think of = as the word “get” and == as “equals”.
So for your line let dLG = draft.syntax.name; I read this in my head as "The variable dLG gets the value of draft.syntax.name".
For the line sylumer wrote if (dLG == "Taskpaper"), I then read it as “If dLG equals “Taskpaper””
It helped me early on in my career, maybe it’ll help you (or anyone else).
Usually, “=” is referred to as (an) assignment, “==” as equality (same value), and “===” as strict equality (same value and same type). Not to dissuade anyone from developing their own interpretations and understanding (it is invaluable to do so), but for someone who is learning, I would argue the case that it is better to use those terms as it then helps you locate and reference documentation about them, particularly given that there are more assignment and (in)equality options that can be applied.
Thanks sylumer. I only mentioned my approach as this was what helped me grasp the concepts of assignment and equality in code syntax when I was just starting out. Of course, if one can sort it out using the official naming conventions, I definitely encourage them to stick with that.