Select Word

Script key: Select Word

Posted by Xor

Install

Details

Label
Voice Over Select Word
Script
var text = getText()
var selRange = getSelectedRange()
var p = selRange[0]
var q = p + selRange[1]

var n = text.length
var i, j

var delim = /^[\s.\/\\()"'\-:,.;<>~!@#$%^&*|+=[\]{}`~?,。、?!.…—-–•/:;()“”「」『』"【】{}#%‰^*+=_—\|~〈〉《》<>&·°⋯'‘’]$/
//var delim = /^[\s.\/\\()"'\-:,.;<>~!@#$%^&*|+=[\]{}`~?]$/
//var delim = /^[\W]$/

selectWord()

if (i === j) {
  //delim = /^[\s\w]$/
  var delim = /^[^.\/\\()"'\-:,.;<>~!@#$%^&*|+=[\]{}`~?,。、?!.…—-–•/:;()“”「」『』"【】{}#%‰^*+=_—\|~〈〉《》<>&·°⋯'‘’]$/
  selectWord()
  if (i === j) {
    delim = /^[\S]$/
    selectWord()
  } else {
    if (i && i === p) --i
    else if (j < n - 1 && j === q) ++j
  }
}

if (j-i)
  setSelectedRange(i, j-i)
/*else if (i)
  setSelectedRange(i-1, 1)
else 
  setSelectedRange(0, 1)*/



function selectWord() {
  i = p
  while (i > 0) {
    if (delim.test(text[i - 1]))
      break
  
    --i
  }
  
  j = q
  while (j < n) {
    if (delim.test(text[j]))
      break
    ++j
  }
}

Advanced

Keyboard shortcut Command+D
Hide key Yes

Discussion

comments powered by Disqus