Need help with a script. Variable scope problem?

I am working on a modified version of @jessegrosjean’s script here.

Instead of incrementing/decrementing a start date, I am wanting a script to just set the start date to a date from a variable defined outside of the TaskPaper function. I am running into a problem.

This codes works (selectedDate variable defined locally in the TaskPaper function):

function TPContextGetCurrentPath(editor, options) {
    var selection = editor.selection;
    var selectedItems = selection.selectedItems;
	
    var selectedDate = "2000-12-17";
	
    editor.outline.groupUndoAndChanges(function() {
        selectedItems.forEach(function(each) {

            var attributeName = 'data-' + options.tag;
            var dueDate = each.getAttribute(attributeName);
            each.setAttribute(attributeName, selectedDate);
        });
    });

    editor.moveSelectionToItems(selection);
}


var path = Application("TaskPaper").documents[0].evaluate({
    script: TPContextGetCurrentPath.toString(),
    withOptions: {
        tag: 'start'
    }
});

But this does not work (selectedDate variable defined globally outside the TaskPaper function):

var selectedDate = "2000-12-17";

function TPContextGetCurrentPath(editor, options) {
    var selection = editor.selection;
    var selectedItems = selection.selectedItems;
	
	
	
    editor.outline.groupUndoAndChanges(function() {
        selectedItems.forEach(function(each) {

            var attributeName = 'data-' + options.tag;
            var dueDate = each.getAttribute(attributeName);
            each.setAttribute(attributeName, selectedDate);
        });
    });

    editor.moveSelectionToItems(selection);
}


var path = Application("TaskPaper").documents[0].evaluate({
    script: TPContextGetCurrentPath.toString(),
    withOptions: {
        tag: 'start'
    }
});

On the face of it, it seems like a scope problem, but I would think the function in the second script would inherit the variable declared globally. What am I missing here?

Your variable is global to the JavaScript to Automation context, but TaskPaper is only evaluating a string version of the TPContextGetCurrentPath function, so your variable line is not known to or seen by the TaskPaper context at all.

If you want to pass values in to the TaskPaper evaluation context, you can to that through the object passed to WithOptions, and seen in the TP context as the second argument (options) of the TPContextGetCurrentPath function

1 Like

Ah. Thank you so much! That clears things up.

Just for sharing, I now have the following, and it works. I had to make sure to call .toString() on the selectedDate variable in the withOptions object to pass in the string value of that variable.

var selectedDate = "2000-12-17";	

function TPContextGetCurrentPath(editor, options) {
    var selection = editor.selection;
    var selectedItems = selection.selectedItems;
	
	
	
    editor.outline.groupUndoAndChanges(function() {
        selectedItems.forEach(function(each) {

            var attributeName = 'data-' + options.tag;
            var dueDate = each.getAttribute(attributeName);
            each.setAttribute(attributeName, options.selectedDate);
        });
    });

    editor.moveSelectionToItems(selection);
}


var path = Application("TaskPaper").documents[0].evaluate({
    script: TPContextGetCurrentPath.toString(),
    withOptions: {
        tag: 'start',
        selectedDate: selectedDate.toString()		
    }
});
2 Likes