Moving tags to end of string

I’m using doing by Bret Terpstra and anyone using it will know the tags can get messy (if you use autotagging etc), Some tags get placed at the start of the string and sometimes when adding tags with a taskpaper client the tags get added in the middle. So here’s my first contribution, a way to move all tags to the end of the body string. If anyone has a better suggestion I would love to hear about it.

function TaskPaperContextScript(editor, options) {
    function doAttribute(editor, item, name) {
        var outline = editor.outline;
        var selection = editor.selection;
        var myvalue = item.getAttribute(name);
        outline.groupUndoAndChanges(function () {
            item.removeAttribute(name);
            item.setAttribute(name, myvalue);
        });
        editor.moveSelectionToItems(selection);
    }

    function toggleAttribute(editor, items) {
        items.forEach(function (itm) {
            itm.attributeNames.forEach(function (myattrib) {
                doAttribute(editor, itm, myattrib);
            });
        });
    }

    toggleAttribute(editor, editor.selection.selectedItems);


}

Application("TaskPaper").documents[0].evaluate({
    script: TaskPaperContextScript.toString()
});
1 Like

Thanks for sharing! I think the approach is sound. It can maybe be a little shorter and more efficient by putting all changes into a single group like this:

function TaskPaperContextScript(editor, options) {
    let outline = editor.outline
    let selection = editor.selection
	
    outline.groupUndoAndChanges(() => {
        editor.selection.selectedItems.forEach((item) => {
            item.attributeNames.forEach((name) => {
		        let value = item.getAttribute(name)
				item.removeAttribute(name)
				item.setAttribute(name, value)			
            });
        });
	})
	
	editor.moveSelectionToItems(selection)
}

Application("TaskPaper").documents[0].evaluate({
    script: TaskPaperContextScript.toString()
});
1 Like