Created
April 10, 2018 11:11
-
-
Save TellMori/1baa4c7dfd0d98c0827c6fa33ce99169 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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