Is there a way to check if current line selected has a tag attached in code?

I want to create a KM macro that will first check if the current line I have selected has any kind of tag attached to it, if yes, it will remove it, if not, it will add a tag @ask for me.

So given this:

57

Pressing the macro would do:

57

And given:

58

Macro would add the tag instantly:

58

I can’t quite figure out how to do the check whether the tag exists (ideally there is some applescript/jxa I can run that would return true or false).

And I also can’t find code to quickly add a tag. There is hotkey for removing tags thankfully.

I only have this macro for adding a tag to end but that is not ideal.

59

Thank you for any help.

Here’s a script that will do that I think:

function TaskPaperContext(editor, options) {
	let outline = editor.outline
	let selection = editor.selection
	let currentItem = selection.startItem
	if (!currentItem) {
		return
	}
	
	outline.groupUndoAndChanges(function() {
		var removedTags = false;
		for (let each of currentItem.attributeNames) {
			if (each.startsWith("data-") && each != "data-type") {
				currentItem.removeAttribute(each);
				removedTags = true;
			}
		}
		if (!removedTags) {
			currentItem.setAttribute("data-ask", "");
		}
	});

	editor.moveSelectionToItems(selection)
}

Application('TaskPaper').documents[0].evaluate({
  script: TaskPaperContext.toString()
});
2 Likes

Thank you :orange_heart: