Action

extended drafts search

Posted by FlohGro, Last update about 2 years ago

UPDATES

about 2 years ago

reverted disabled regex search

show all updates...

about 2 years ago

reverted disabled regex search

about 2 years ago

disabled regex search - if you want to enable it - follow the instructions in the script step of the action

about 2 years ago

name change and description update

created by @FlohGro / more on my Blog

extended drafts search

This actions gives you some options to search through your drafts library which are not possible with the standard search or the quick search view.

First you can search through your library by

  • the title of the drafts
  • the whole content of drafts

Additionally you can also search through all your drafts with a regex expression.

Last but not least you can search for a tag - select the tag you wanted and query for all drafts with that tag.

[Configuration]

You can just use the action as it is. If you don’t need one of the search types you can modify this line const searchTypes = ["in titles", "in content", "as regex", "in tags"]; and just remove e.g. „“as regex”,“* *from the list.

[Usage]

Just run the action anytime you want to search in you drafts library. It will display a prompt where you can type your query and then press the button to select the type of your search. If the resulting prompt shows the draft you searched for just select it and it will be opened. If you didn’t found what you searched for just „cancel“ the draft selection prompt and the action will ask you if you want to perform another search.


If you find this useful and want to support me you can

Buy Me A Coffee

Steps

  • script

    // search drafts
    
    // regex removed since its also available in the quick search - if you want to enable regex search just comment out line 4 and comment in line 5
    //const searchTypes = ["in titles", "in content", "in tags"];
    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
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.