Action

Double Bracket Search

Posted by David_Crandall, Last update about 5 years ago

Description

This action is an advanced search function meant to mimic the double bracket searching found in applications like nvALT and Bear. Note however, it overrides the double bracket search if any text is selected and prompts for a search term if no other conditions are found.

Search Order

The search order is as follows:

  1. If a selection is made, search for that
  2. If no selection is made but one double bracket exists, search for that
  3. If multiple double brackets exist, prompt for which one to search
  4. Else, prompt for a search term

Recommendation

I recommend mapping this action to a hot key combo like command+alt+F. It makes it easier to use quickly. At least, that’s what I’ve done.

Steps

  • script

    // Double Bracket Search
    // Search for selected text first, then double brackets
    
    // Modifiable parameters
    let workspaceName = "[[?]]"; // Name of the search workspace
    let tagFilter = "!script"; // tag filters; use "!" to omit tags
    
    // Identify selection
    var sel = editor.getSelectedText();
    var selRange = editor.getSelectedRange();
    var dContent = draft.content;
    
    // Bracket Regex
    const findRegex = /\[\[(.*?)\]\]/g; //  /(\[\[)([0-9]{1,8})(\]\])/g;
    var findRegexS = String(findRegex);
    var matches = dContent.match(findRegex)
    var search = String(matches);
    
    // Search scenarios
    // 1. Use selection if available
    if (sel.length > 0) {
    	var queryTerm = sel;
    	}
    
    // 2. Look for double brackets
    else if (matches !== null) {
    	// 2a. If multiple sets of double brackets, prompt for which one to search
    	if (matches.length > 1) {
    		var p = Prompt.create();
    		p.title = "Choose";
    		p.message = "Pick a phrase to search for";
    
    		// Parse multiple brackets into separate options for prompt
    		for(i=0; i<matches.length; i++) {
    			p.addButton(matches[i])
    			}
    
    		// Show prompt
    		p.show();
    		var queryTerm = p.buttonPressed;
    		queryTerm = queryTerm.substring(2, queryTerm.length - 2);
    		}
    
    	// 2b. If only one set of double brackets, skip prompt
    	else if (matches.length == 1) {
    		var queryTerm = search.substring(2, search.length - 2);
    		}
    }
    
    // 3. If no selection and no double brackets, prompt for search term
    else if (!sel || sel.length == 0) {
    	var p = Prompt.create();
    	p.title = "Search";
    	p.message = "Enter your search terms";
    	p.addTextField("searchTerms", "Terms", "");
    	p.addButton("Search");
    	//p.isCancellable = false;  <-- Kill this too
    	p.show();
    	
    	var queryTerm = p.fieldValues["searchTerms"];
    	}
    	
    // 4. Should never reach this point BUT lets you know if a scenario was missed
    else {
    	alert("Something went wrong. :(");
    	var queryTerm = "";
    }
    
    let queryString = queryTerm;
    
    // Create temporary search workspace
    let ws = Workspace.create();
    ws.name = workspaceName;
    ws.tagFilter = tagFilter;
    ws.queryString = queryString;
    ws.setInboxSort("created", true);
    ws.showPreview = false;
    ws.showLastAction = false;
    
    app.applyWorkspace(ws);
    app.showDraftList();

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.