Skip to content

Instantly share code, notes, and snippets.

@TellMori
Created April 10, 2018 11:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TellMori/1baa4c7dfd0d98c0827c6fa33ce99169 to your computer and use it in GitHub Desktop.
Save TellMori/1baa4c7dfd0d98c0827c6fa33ce99169 to your computer and use it in GitHub Desktop.
app = Application("CotEditor")
app.includeStandardAdditions = true
doc = app.documents()[0]
function replaceSelectedText1(newText) {
selectedRange = doc.selection.range()
if(!selectedRange[1]){
// omit
return newText
}
baseIndex = selectedRange[0] + 1
numberOfCharacters = selectedRange[1]
while(--numberOfCharacters > 0)
doc.text.characters[baseIndex] = ""
doc.text.characters[--baseIndex] = newText
doc.selection.range = [baseIndex, newText.length]
return newText
}
function replaceSelectedText2(newText) {
selectedRange = doc.selection.range()
if(!selectedRange[1]){
// omit
return newText
}
theText = doc.text()
beginIndex = selectedRange[0];
afterIndex = beginIndex + selectedRange[1]
beforeText = (beginIndex == 0)? "" : theText.substring(0, beginIndex)
afterText = (afterIndex == theText.length)? "" : theText.slice(afterIndex)
doc.text = beforeText + newText + afterText
doc.selection.range = [beginIndex, newText.length]
doc.scrollToCaret()
return newText
}
function selectedText() {
selectedRange = doc.selection.range()
if(!selectedRange[1])
return ""
return doc.text().substring(selectedRange[0], (selectedRange[0] + selectedRange[1]))
}
replaceSelectedText2(selectedText().replace(/[A-Z]*/g, ""))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment