Action

add tag from category

Posted by FlohGro, Last update over 1 year ago

created by @FlohGro / more on my Blog

add tag from category

add tags from configured categories to the current draft.

[Configuration]

Configure the categories = prefixes for the tags at the beginning of the script to your preferences

If you find this useful and want to support me you can

Buy Me A Coffee

Steps

  • script

    var categoryList = ["1 ", "2 ", "3 ", "4 ", "#", ":", "@"];
    
    function allTags() {
      return [...new Set([].concat(...Draft.query("", "all", []).map((currentDraft) => currentDraft.tags)))].sort();
    }
    
    var addAnotherTag = true;
    
    function run() {
    
      categoryList.sort();
      //if a new tag is created, remove spaces in the text (if "false", spaces are allowed)
      const removeSpacesInTags = true;
      var category;
    
      var p1 = Prompt.create();
      p1.title = "select category";
      p1.message = "draft: " + draft.displayTitle;
      for (var cat of categoryList) {
        p1.addButton(cat);
      }
    
      if (p1.show()) {
        category = p1.buttonPressed;
        addAnotherTag = true;
      } else {
        addAnotherTag = false;
      }
    
    
      if (addAnotherTag == true) {
        var matchedTags = [];
        // get all tags into Set
        /*var tags = new Set();
        var ws = Workspace.create();
        var leftDrafts = ws.query("all");
        while (leftDrafts.length > 0) {
          if (leftDrafts[0].tags != 0) {
            let thisDraftsTags = leftDrafts[0].tags;
            for (tag in thisDraftsTags) {
              tags.add(thisDraftsTags[tag]);
            }
          }
          leftDrafts.shift();
        }*/
        var tags = allTags();
    
        for (tag of tags) {
          if (tag.startsWith(category)) {
            // only show tags not already assigned to the draft.
            if (!draft.tags.includes(tag)) {
              matchedTags.push(tag);
            }
          }
        }
        var p = Prompt.create();
        p.title = "Select Tags";
        p.message = "from category " + category;
    
        for (tag of matchedTags) {
          p.addButton(tag);
        }
        p.addButton("create new tag in category");
    
        var con = p.show();
    
        if (con) {
          var sel = p.buttonPressed;
          if (sel != "create new tag in category") {
            draft.addTag(sel);
            draft.update();
          } else {
            var p3 = Prompt.create();
            p3.title = "new tag";
            p3.message = "new tag in category " + category;
            p3.addTextField("newTag", "create tag", "");
            p3.addButton("create tag");
    
            if (p3.show()) {
              let setTag = p3.fieldValues["newTag"];
              if (removeSpacesInTags & setTag.includes(' ')) {
                setTag = setTag.replace(/\s+/g, '');
              }
              if (setTag.length != 0) {
                if (category != "#") {
                  draft.addTag(category + setTag);
                } else {
                  draft.addTag(category + setTag);
                }
                draft.update();
              } else {
                alert("tag with length 0 can't be added to category!");
                context.fail("tag with length 0 can't be added to category!")
              }
            } else {
              context.cancel();
            }
          }
        } else {
          context.cancel();
        }
    
      }
    }
    
    while (addAnotherTag == true){
    	run();
    	}
    
    
    
    
    var p4 = Prompt.create();
    p4.title = "what2do?";
    
      p4.addButton("FLAG");
        p4.addButton("FLAG & ARCHIVE");
      p4.addButton("NOTHING");
      p4.addButton("ARCHIVE");
    
      if (p4.show()) {
      	var selection = p4.buttonPressed;
      	
      	if(selection == "FLAG"){
      		draft.isFlagged = true;
      		draft.update();
      	} else if (selection == "FLAG & ARCHIVE"){
      		draft.isFlagged = true;
      		draft.isArchived = true;
      		draft.update();
      	} else if (selection == "NOTHING"){
      	
      	} else if (selection == "ARCHIVE"){
      		draft.isArchived = true;
      		draft.update();
      	}
      }

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.