Action

Tag Filter

Posted by @derekvan, Last update over 4 years ago

This action uses Matt Gemmell’s CheckListPrompt script and action to create a series of prompts that ask you to choose a set of tags, then choose a draft to load in the editor.

You need to have this action installed for it to work.

Steps

  • includeAction

    name
    MGCheckListPrompt Library
  • script

    // Tag Prompt / Filter
    
    // Get all Tags from all Drafts
    
    const drafts = Draft.query("","all",[]);
    
    const allDrafts = [];
    
    for (let d of drafts)
    {
        let tags = d.tags;
        for (let x in tags)
        {
            allDrafts.push(tags[x]);
        }
    }
    
    const allTags = Array.from(new Set(allDrafts));
    allTags.sort();
    
    // Create dictionary of Tags for the Prompt to process
    
    var promptDict = [];
    
    allTags.forEach(element => promptDict.push({"title":element}));
    
    
    
    // Create MGChecklist
    
    var prompt = new MGCheckListPrompt();
    prompt.message = "Filter by which tags?";
    prompt.title = "Tags";
    prompt.allowsTypeToSelect = true;
    prompt.addItems(promptDict);
    
    var selectedItems = prompt.show();
    
    // Create Tag array for querying
    if (prompt.didShow) {
    	if (selectedItems != null) {
            
            var sTags = [];
            for (let s of selectedItems)
            {
                sTags.push(promptDict[s].title);
            }
    	
    
    
    var tagged = Draft.query("","all",sTags);
    
    // Create new Prompt for showing Drafts matching the Tag query
    
    var selDrafts = new MGCheckListPrompt();
    selDrafts.title = "Tagged Drafts";
    selDrafts.allowsTypeToFilter = true;
    selDrafts.singleSelectionMode = true;
    selDrafts.selectsImmediately = true;
    selDrafts.addDrafts(tagged);
    
    var sItems = selDrafts.show();
    
    // Load the selected Draft
    
    if (selDrafts.didShow) {
    	if (sItems != null) {
            for (let d of sItems)
            {
                editor.load(tagged[d]);
            }
    	} else {
            app.displayInfoMessage("Prompt was cancelled.");
            context.cancel();
    	}
    } else {
        app.displayErrorMessage("Something went wrong.");
        context.cancel();
    }
    
    } else {
        app.displayInfoMessage("Prompt was cancelled.");
        context.cancel();
    }
    } else {
    app.displayErrorMessage("Something went wrong.");
    context.cancel();
    }

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.