Possible to add date to a different tag than done?


#1

i like using a “waiting” tag for items, besides using a km macro, is that possible natively?


#2

Here’s a script that I think starts to do what you want:

function TaskPaperContextScript(editor, options) {
  var today = DateTime.format('today');
  var outline = editor.outline;
    
  outline.groupUndoAndChanges(function() {
    editor.selection.selectedItems.forEach(function (each) {
      if (!each.hasAttribute('data-waiting')) {
        each.setAttribute('data-waiting', today);
      }
    });
  });
}

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

#3

i was kind of hoping that when adding the waiting tag it would auto insert the date like marking a task as done.


#4

Can you describe the exact sequence of events that you are looking for. It should be that when you run this script @waiting(2016-03-02) is inserted. Just like when you tag an item done @done(2016-03-02).


#5

Would it be possible to add a toggle function, so the same keyboard shortcut can either add the waiting tag if it doesn’t exist, or remove the waiting tag if it already does?


#6

This script is a bit more complicated, but it should work exactly like the Toggle Done command, except with a different tag… (I think!)

function TaskPaperContextScript(editor, options) {

  function setAttribute(editor, items, name, value) {
    var outline = editor.outline;
    var selection = editor.selection;	
    outline.groupUndoAndChanges(function() {
      items.forEach(function (each) {
        each.setAttribute(name, value);
      });
    });	
    editor.moveSelectionToItems(selection);
  }

  function toggleAttribute(editor, items, name, value) {
    items.forEach(function (each) {
      if (each.hasAttribute(name)) {
        value = null; // If any item has the attribute, then remove it from all.
      }
    });
    setAttribute(editor, items, name, value);
  }

  toggleAttribute(editor, editor.selection.selectedItems, 'data-waiting', DateTime.format('today'));
}

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

#7

Just tested it and it does — brilliant! Thank you so much. :panda_face:


#8

nice!

any thoughts on removing all tags when a task is marked as done? :wink:


#9

I don’t think I’d like to do that for the default “Done” command. But (as often) it’s easy to do in a script. In fact the data-waiting script is a great starting point, you’d only need to make a few changes. But I want you to try this one :smile:. Here’s what you’d have to do:

  1. Change data-waiting to data-done since you are toggling the @done tag instead of @waiting tag.

  2. In the case when @done is being added added (value != null) also remove all the other tags.

  3. Use each.attributeNames to get all item attribute names. (each is an Item instance)

  4. You only want to remove the attributes that represent @tags. (there are other attributes associated with items such as indent). You can tell if an attribute name is associated with a tag by checking to see if the name starts with data-. So for each each.attributeNames that starts with data- then call each.removeAttribute.

  5. The scripting API is documented at http://guide.taskpaper.com/scripting_api.html. If it doesn’t go smoothly please post any questions here!


#10

challenge accepted! i think i’ll appreciate this help much more! :wink:


#11

challenge still sccepted, I’ve just been on a crash course learning Python for my 3-D modeling program