⌘F { [¶] | [[s]] }
Find current line or selected text.

Script key: Find current line or selected text.

Posted by cprichard

Install

Details

Label ⌘F { [¶] | [[s]] }
Voice Over Find current line or selected text.
Script
// Script keys run short Javascripts
// For documentation and examples, visit:
// http://help.agiletortoise.com
// https://drafts4-actions.agiletortoise.com/k/2Pp
// Derived from http://drafts4-actions.agiletortoise.com/k/1gE

// Find the current line starting at the beginning of the draft or find the selected text beginning at the cursor.

var search_term, search_index, line_range, match, length;

if ( isSelect() === true ) {
  search_term = getSelectedText();
  search_index = getSelectedRange()[0] + 1;
} else {
  line_range = getSelectedLineRange();
  search_term = getTextInRange( line_range[0], line_range[1] );
  search_index = 0;
}
// search_term = search_term.replace( /\r?\n/g, '' );
// match = getText().indexOf( search_term, search_index );
// length = search_term.length;

// Convert to lowercase to find all instances
search_term = search_term.replace( /\r?\n/g, '' );
search_term = search_term.toLowerCase();
searchIn = getText();
searchIn = searchIn.toLowerCase();
match = searchIn.indexOf( search_term, search_index );
length = search_term.length;

if ( match > 0 ) {
  setSelectedRange( match, length );
  // alert("Found!");     // you might have to scroll to see the selected text
} else {
  setSelectedRange( search_index-1, length );
  alert("Not Found!"); // selected text will remain selected
}

function isSelect() {
  if ( getSelectedText().length > 0 ) {
    return true;
  } else {
    return false;
  }
}

Advanced

Keyboard shortcut Control+f
Hide key Yes

Discussion

comments powered by Disqus