Need help with if and else statement in a script

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.


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);

Keep in mind languageGrammar is a deprecated property and you probably want to work with syntax now.

I suspect that your issue lies in you not testing for equality in your conditions; “=” is not an equality test.

1 Like

I have read the article you linked and I sort of understand what == does, but have no idea how to include it in the script.

You would need to apply it in the equality checks you were intending to make:

if (dLG == "Taskpaper")
else if (dLG == "Markdown") {

Note that the point about the deprecation of languageGrammar still stands.

You may also wish to account for other Markdown syntax flavours.

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

Yes, modified it right away.

let dLG =;

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 =; I read this in my head as "The variable dLG gets the value of".

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.

For example:

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.

Great explanation! I’m aware of that I need to read more about JavaScript.