Action

Tag Assign or Filter

Posted by @derekvan, Last update over 5 years ago

Combines Tim Nahumck’s excellent “update tags” action (https://www.macstories.net/reviews/drafts-5-the-macstories-review/3/#fn-54021-generous) and Greg’s excellent “temporary workspace” action (https://forums.getdrafts.com/t/tip-use-temporary-workspaces-to-filter-the-draft-list/3030).

Basically, you get a menu of tags and you can select some tags which will either a) update the current draft’s tags based on your selection; or b) filter the drafts list by those tags.

You can also write in new tags via the “other” field; write a search query via the search field; and archive the draft after updating its tags.

Edit the script on line 6 to write in your own tags

Steps

  • script

    // choose tags or filter by tags 
    // show multi-select with current assigned tags
    // and a static list of common tags
    
    // baseTags are always visible.
    var baseTags = ["tag1", "tag2", "tag3"];
    
    var currentTags = draft.tags;
    var allTags = baseTags.concat(currentTags);
    var selectableTags = Array.from(new Set(allTags));
    
    // create prompt to display
    var p = Prompt.create();
    p.title = "Assign tags";
    
    p.addSelect("tags", "", selectableTags, currentTags, true);
    
    p.addTextField("othertags", "Other", "", {
      "placeholder": "comma-separated tags",
      "autocapitalization": "none"
    });
    p.addTextField("qs","Query","", {
      "placeholder": "type search terms here",
      "autocapitalization": "none"
    });
    p.addSwitch("archive","Archive?",false);
    p.addButton("Update");
    p.addButton("Filter");
    
    if (p.show()) {
    	if (p.buttonPressed == "Update") {
    		var selectedTags = p.fieldValues["tags"];
       		for (var tag of currentTags) {
       			draft.removeTag(tag);
      			}
    		for (var tag of selectedTags) {
    			draft.addTag(tag);
    			}
    		var otherTags = p.fieldValues["othertags"].split(",");
    		for (var tag of otherTags) {
    			draft.addTag(tag);
    			}
    	
    		draft.update();
    	}
    	if (p.fieldValues["archive"] == true) {
       		draft.isArchived = true;
      	    draft.update(); 
    	}
    
    else if (p.buttonPressed == "Filter") {
    // BEGIN config variables
    // setup tags or searches you wish to load...
    let name = "Tag Filter";
    var selectedTags = p.fieldValues["tags"];
    var tagfilter = selectedTags.toString();
    var qs = p.fieldValues["qs"];
    // END config variables
    
    // create workspace
    // for other options, see:
    // https://reference.getdrafts.com/objects/Workspace.html
    let ws = Workspace.create();
    ws.name = name;
    ws.tagFilter = tagfilter;
    if (qs) {
    	ws.queryString = qs;
    	}
    ws.setInboxSort("modified", true);
    
    // unless you call `ws.update()`, this ws is temporary
    // load this workspace, and display draft list
    
    app.applyWorkspace(ws);
    app.showDraftList();
    }}

Options

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