Action

Tagger

Posted by Harshal Shinde, Last update 8 days ago

Tagger Overview:

This Drafts Action allows efficient tag management, helping you organize your drafts with precision. It reads a pre-created “master tag list” (stored as a separate draft where each tag is listed on a new line) and presents these tags in a selectable list. You can use Multiple Keyword Filtering by separating your search terms with commas. For instance, typing fru will filter the list to show tags such as [sne*fru*], [jack*fru*its], and [*fru*gal].

Note:

You need to input the UUID of your “master tag list” draft in the second line of the second script:

Draft.find("UUID")


Process Workflow

  1. Decide on Existing Tags:

    • Choose to either keep or remove the tags currently assigned to the draft.
    • Enter comma-separated filter keywords to narrow down the master list.
    • Leave the input field blank to display the full tag list.
  2. Filter Tags:

    • Apply the filter keywords to the master tag list.
    • Retain the currently assigned tags in the final list only if you chose to keep them in Step 1.
  3. Tag Selection:

    • View the filtered list of tags, including any retained ones.
    • Select from the filtered options and, optionally, manually add additional tags.
  4. Update the Draft:

    • Once your tags are finalized, update the draft with the selected tags along with any manually added ones.

Steps

  • script

    
    // Get current tags before modification
    var currentTags = draft.tags.slice(); // Clone to avoid live changes
    
    // Create a prompt to ask whether to clear existing tags
    var prompt = Prompt.create();
    prompt.title = "Manage Existing Tags";
    prompt.message = "Do you want to keep or remove the current tags?";
    
    prompt.addButton("Keep Tags");
    prompt.addButton("Remove Tags");
    
    if (prompt.show()) {
        if (prompt.buttonPressed === "Remove Tags") {
            for (var tag of currentTags) {
                draft.removeTag(tag);
            }
            draft.update(); // Ensure tags are removed before proceeding
        }
    }
    
    // Run UniversalTags.js
    app.queueAction(Action.find("UniversalTags"), draft);
    
  • script

    // Get the draft with tags
    var tagsDraft = Draft.find("D16FB76B-5BA8-4B6D-903B-C612A8026376");
    
    if (!tagsDraft) {
      alert("Could not find the draft with tags");
      context.cancel();
    }
    
    // Parse tags from the draft (assuming each tag is on a separate line)
    var availableTags = tagsDraft.content.split("\n").filter(function(line) { 
      return line.trim().length > 0; 
    });
    
    // Get current tags of the draft
    var currentTags = draft.tags;
    
    // Create a complete list of tags (ensuring no duplicates)
    var selectableTags = Array.from(new Set(availableTags));
    
    // Step 1: Prompt the user for filter keywords
    var filterPrompt = Prompt.create();
    filterPrompt.title = "Filter Tags";
    filterPrompt.addTextField("filterKeywords", "Enter filter keywords (comma-separated)", "", {
      "placeholder": "Type keywords..."
    });
    filterPrompt.addButton("Continue");
    
    if (filterPrompt.show()) {
      // Get the filter keywords entered by the user
      var filterKeywords = filterPrompt.fieldValues["filterKeywords"]
        .toLowerCase()
        .trim()
        .split(",")
        .map(function(keyword) { return keyword.trim(); });
    
      // If filterKeywords is not empty, filter the tags based on them
      var filteredTags = filterKeywords.length > 0
        ? selectableTags.filter(function(tag) {
            return filterKeywords.some(function(keyword) {
              return tag.toLowerCase().includes(keyword);
            });
          })
        : selectableTags; // Show all tags if no keywords are entered
    
      // Include current tags (even if not matching filter) in the filtered list
      var filteredTagsWithCurrent = Array.from(new Set(filteredTags.concat(currentTags)));
    
      // Create prompt to display
      var p = Prompt.create();
      p.title = "Assign Tags";
      p.addSelect("tags", "", filteredTagsWithCurrent, currentTags, true);
      p.addTextField("othertags", "Other", "", {
        "placeholder": "comma-separated tags",
        "autocapitalization": "none"
      });
      p.addButton("Update");
    
      // Show the prompt
      if (p.show()) {
        var selectedTags = p.fieldValues["tags"];
        
        // Remove current tags
        for (var tag of currentTags) {
          draft.removeTag(tag);
        }
        
        // Add selected tags
        for (var tag of selectedTags) {
          draft.addTag(tag);
        }
        
        // Process any other tags entered manually
        var otherTags = p.fieldValues["othertags"].split(",");
        for (var tag of otherTags) {
          // Only add non-empty tags
          if (tag.trim().length > 0) {
            draft.addTag(tag.trim());
          }
        }
        
        draft.update();
        
        // Calculate total tags added for confirmation
        var totalTagsAdded = selectedTags.length;
        var otherTagsAdded = otherTags.filter(function(tag) { return tag.trim().length > 0; }).length;
        totalTagsAdded += otherTagsAdded;
        
        alert("Tags updated. Added " + totalTagsAdded + " tag(s).");
      }
    }
    

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.