Action

Create filtered copy

Posted by derick, Last update over 6 years ago

Creates a copy of the current draft containing lines that match a string or regex. Copy will have the same tags and syntax as the original. Optionally add a tag with the filter.

Aspires to grow up to be grep.

Steps

  • script

    // See online documentation for examples
    // http://getdrafts.com/scripting
    
    var p = Prompt.create();
    p.title = "Filter";
    p.addTextField("f","","");
    p.addButton("Enter");
    p.addSwitch("tagit","Add filter: tag?",true);
    p.addSwitch("regex","Use Regular Expressions?",true);
    
    if (p.show()) {
    
    
    var dc = draft.content.split("\n")
    
    if (p.fieldValues["regex"]) {
    var f = RegExp(p.fieldValues["f"]);
    dc = dc.filter(s => f.test(s));
    } else {
    var f = p.fieldValues["f"];
    dc = dc.filter(s => s.includes(f));
    }
    // copy over tags and add filtered tag
    
    var d=Draft.create();
    d.content=dc.join("\n");
    d.languageGrammar = draft.languageGrammar;
    
    var ft = "filter:"+(p.fieldValues["f"])
    
    if (p.fieldValues["tagit"]) {
    d.addTag(ft);
    }
    draft.tags.forEach(t => d.addTag(t));
    
    d.update();
    
    editor.load(d)
    }

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.