search drafts

Posted by @FlohGro, Last update 18 days ago

The Bretter search for Drafts.

You can search through your Drafts List by:
- title
- content
- regex
- tags (First find a tag and they select a draft with this tag

Steps

  • script

    // search drafts
    const searchTypes = ["in titles", "in content", "as regex", "in tags"];
    var foundDrafts = [];
    var runSearch = true;
    var searchTypeDesctiptor = "";
    
    
    function queryByTitle(query) {
      let searchResults = [];
      let allDrafts = Draft.query(query, "all", [], [], "modified", true, true);
      for (d of allDrafts) {
        if (d.title.includes(query)) {
          searchResults.push(d);
        }
      }
      return searchResults;
    }
    
    function queryByString(query) {
      let searchResults = Draft.query(query, "all", [], [], "modified", true, true);
      return searchResults;
    }
    
    
    function queryByRegex(query) {
      let searchResults = [];
    
      let allDrafts = Draft.query("", "all", [], [], "modified", true, true);
    
      for (d of allDrafts) {
        if (d.content.match(query)) {
          searchResults.push(d);
        }
      }
      return searchResults;
    }
    
    function queryByTags(query, isTagSearch) {
      let searchResults = [];
    
      if (isTagSearch) {
        let allDrafts = Draft.query("", "all", [], [], "modified", true, true);
        for (d of allDrafts) {
          for (tag of d.tags) {
            if (tag.includes(query)) {
              if (!searchResults.includes(tag)) {
                searchResults.push(tag);
              }
            }
          }
        }
      } else {
        let allDrafts = Draft.query("", "all", [query], [], "modified", true, true);
        for (d of allDrafts) {
          searchResults.push(d);
        }
      }
    
      return searchResults;
    }
    
    
    function selectFoundTag(tagsList) {
      tagsList.sort();
      let pSelectTag = Prompt.create();
      for (tag of tagsList) {
        pSelectTag.addButton(tag);
      }
      if (pSelectTag.show()) {
        return pSelectTag.buttonPressed;
      } else {
        return "NO TAG SELECTED";
      }
    }
    
    function searchAgain(msg){
    	let pSearchAgain = Prompt.create();
    	pSearchAgain.title = "search again?"
    	pSearchAgain.message = "because " + msg;
    	pSearchAgain.addButton("YES",true);
    	pSearchAgain.addButton("NO",false);
    	pSearchAgain.isCancellable = false;
    	pSearchAgain.show();
    	return pSearchAgain.buttonPressed;
    
    }
    
    
    
    function selectResultToOpen(foundDrafts) {
      pOpenDraft = Prompt.create();
      pOpenDraft.title = "select draft to open";
    
      for (d of foundDrafts) {
        pOpenDraft.addButton(d.title, d);
      }
    
      if (pOpenDraft.show()) {
        editor.load(pOpenDraft.buttonPressed);
        runSearch = false;
      } else {
    	runSearch = searchAgain("you did not select to open a draft in the " +         searchTypeDesctiptor + " search");
      }
    
    }
    
    function performSearch() {
      // prompt which search
      pSearchBy = Prompt.create();
      pSearchBy.title = "search for";
    
      pSearchBy.addTextField("query", "", "", {
        "wantsFocus": true
      });
    
      for (searchType of searchTypes) {
        pSearchBy.addButton(searchType);
      }
    
    
    
      if (pSearchBy.show()) {
        let query = pSearchBy.fieldValues["query"];
        if (query == "") {
          context.fail("no search query was entered");
          app.displayErrorMessage("no search query was entered");
        } else {
          switch (pSearchBy.buttonPressed) {
            case "in titles":
            	searchTypeDesctiptor = "title";
              foundDrafts = queryByTitle(query);
              break;
            case "in content":
            searchTypeDesctiptor = "content";
              foundDrafts = queryByString(query);
              break;
            case "as regex":
                    searchTypeDesctiptor = "regex";
              foundDrafts = queryByRegex(query);
              break;
            case "in tags":
                    searchTypeDesctiptor = "tag";
              foundDrafts = queryByTags(query, true);
              if (foundDrafts.length > 0) {
                let selectedTag = selectFoundTag(foundDrafts);
                if (selectedTag == "NO TAG SELECTED") {
                  foundDrafts = [];
                  runSearch = searchAgain("you did not select a tag to query for drafts");
                } else {
                  foundDrafts = queryByTags(selectedTag, false);
                }
              }
              break;
            default:
              context.fail("searchType not implemented!");
              break;
          }
          if (foundDrafts.length > 0) {
            selectResultToOpen(foundDrafts);
          } else {
          	               runSearch = searchAgain("no drafts where found with your " +         searchTypeDesctiptor +  " search for \"" + query + "\"")
          }
    
        }
      } else {
        context.cancel();
        runSearch = false;
      }
    }
    
    while(runSearch){
    performSearch();
    }

Options

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