CotEditor supports AppleScript and JXA (JavaScript for Automation).
It has own AppleScript dictionary so that you can look up all the classes or commands available with CotEditor. To open the dictionary:
The original classes and properties that are defined in CotEditor.
view opacitycontentstextlengthselectionencodingIANA charsetShift_JIS, EUC-JP)line endingCR / LF / CRLF)tab widthexpands tabwrap linescoloring styleline spacingcontentsrangeline rangelength can be omitted, one line will be selected even if it were 0 or 0)selectioncontents of selection of document 1
set contents of selection of front document to "Apple"
range of selection of front document
set range of selection of front document to {1, 12}
set line range of selection of front document to 10
set range of selection of front document to {-15, -1}
The selection property doesn't work by itself. Use this property with others such as contents.
When ‘location’ is a negative value, the selection range starts from the ‘location’-th last character.
When ‘length’ is a positive value, the selection range becomes the ‘length’ characters starting from ‘location.’ If ‘length’ is larger than the number of the rest characters in the document, the range will be from ‘location’ to the end.
When ‘length’ is a negative value, the selection range ends at the ‘length’-th last character. If the absolute value of ‘length’ is smaller than ‘location’ (i.e. the selection's end point is before ‘location’), the caret just moves to ‘location’ (same as when {location, 0} was input).
This specifying method is modelled after that of PHP's substr.
The command for changing selection doesn't scroll the window. Use the scroll to caret command to make the selection remain in the view.
The original commands that are defined in CotEditor.
(properties surrounded by [ ] are options)
write to consolewrite to console "Script failed."
findtrue, if any, otherwise returns false.for (Unicode text)RE (boolean)]wrap (boolean)]ignore case (boolean)]backwards (boolean)]The search starts from the current selection (caret position).
For example, when not using the wrap or backwards options and when there are no matching string after the current selection, then false is returned.
The regular expression search cannot search backwards. If both options were specified at the same time, RE takes precedence and backwards is ignored.
find front document for "Apple" with ignore case
replace0.for (Unicode text)to (Unicode text)all (boolean)]RE (boolean)]wrap (boolean)]ignore case (boolean)]backwards (boolean)]As in the case of the find command, the search starts from the current selection (caret position). Use the all option for searching the whole document.
After replacing the whole document using the all option, the caret moves to the head of the document. If there were no matching string, the caret doesn't move.
The regular expression search cannot search backwards. If both options were specified at the same time, the backwards option is ignored.
replace front document for "Apple" to "Orange" with all and ignore case
scroll to caretscroll to caret front document
convertto (Unicode text)lossy (boolean)convert front document to "Unicode (UTF-8)" without lossy
reinterpretas (Unicode text)Returns false if the file has never been saved.
Changes that are not yet saved will be lost.
reinterpret front document as "Japanese (EUC)"
shift leftshift rightshift right selection of front document
comment outuncommentThese commands do nothing if not possible for example in cases where no delimiters are set in the current syntax style or no comment delimiters to remove are available.
comment out selection of front document
stringin (list)Returns an empty string if the specified range was invalid.
This command does not change the specified range.
string front document in {0, 10}
change caseto upper/lower/capitalizedWorks only for ‘selection’ objects.
change case selection of front document to upper
change roman widthto half/fullWorks only for ‘selection’ objects.
change roman width selection of front document to full
change kanato hiragana/katakanaWorks only for ‘selection’ objects.
change kana selection of front document to katakana
smarten quotesstraighten quotesWorks only for ‘selection’ objects.
smarten quotes selection of front document
smarten quotesWorks only for ‘selection’ objects.
smarten dashes selection of front document
normalize unicodeto NFKC/NFD/NFC/NFKD/NFKC Casefold/Modified NFC/Modified NFDWorks only for ‘selection’ objects.
normalize unicode selection of front document to NFC
move line upmove line downWorks only for ‘selection’ objects.
sort linesreverse linesWorks only for ‘selection’ objects.
delete duplicate lineWorks only for ‘selection’ objects.