CotEditor works with AppleScript and JXA (JavaScript for Automation).
It has its own AppleScript dictionary so that you can look up all the classes or commands available with CotEditor. To open the dictionary:
In addition, script hooks can be used to automatically execute scripts in response to specific events. For details, see Run script on specific events.
To confirm the version history of the AppleScript support on CotEditor, see Support different CotEditor versions in AppleScript.
The original classes and properties that are defined in CotEditor.
The CotEditor application.
A CotEditor window.
A CotEditor document.
contentstextlengthselectionencodingIANA charsetShift_JIS, EUC-JP)has BOMline endingCR / LF / CRLF / NEL / LS / PS)tab widthexpands tabwrap linescoloring styleThe current selection.
contentsrangeline rangelength can be omitted, one line is selected even if it were 0 or 1)contents 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.
Starting form CotEditor 5.0, characters are counted in the Unicode grapheme cluster unit. This is the same as the specification of AppleScript 2.0.
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 is 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’ (that is, 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 based on substr in PHP.
The command for changing selection doesn’t scroll through the editor. Use the scroll to caret command or jump command to make the selection remain in the view.
The original commands that are defined in CotEditor.
(properties surrounded by [ ] are options)
write to consoletitle (boolean)]timestamp (boolean)]CotEditor 3.2.0+
write to console "Script failed."
write to console "calculating…" without title and timestamp
findtrue, if any, otherwise returns false.for (text)RE (boolean)]wrap (boolean)]ignore case (boolean)]backwards (boolean)]find front document for "Apple" with ignore case
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.
replace0.for (text)to (text)all (boolean)]RE (boolean)]wrap (boolean)]ignore case (boolean)]backwards (boolean)]replace front document for "Apple" to "Orange" with all and ignore case
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.
scroll to caretscroll to caret front document
jumpto line (int)column (int)]CotEditor 2.1.0+
jump front document to line -1
If a negative value is provided, the line/column is counted from the end.
convertto (text)lossy] (boolean)BOM] (boolean)convert front document to "Unicode (UTF-8)" with BOM without lossy
From CotEditor 4.0.7 on, the new encoding name accepts also IANA charset names.
reinterpretas (text)reinterpret front document as "Japanese (EUC)"
Returns false if the file has never been saved.
Changes that are not yet saved are lost.
From CotEditor 4.0.7 on, the new encoding name accepts also IANA charset names.
shift leftshift rightshift right selection of front document
comment outuncommentCotEditor 2.0.1+
comment out selection of front document
These commands do nothing if not possible for example in cases where no delimiters are set in the current syntax or no comment delimiters to remove are available.
stringin (list)string front document in {0, 10}
Returns an empty string if the specified range was invalid.
This command does not change the specified range.
change caseto upper/lower/capitalizedchange case selection of front document to upper
change roman widthto half/fullchange roman width selection of front document to full
change kanato hiragana/katakanachange kana selection of front document to katakana
smarten quotesstraighten quotesCotEditor 3.9.7+
smarten quotes selection of front document
smarten dashesCotEditor 3.9.7+
smarten dashes selection of front document
normalize unicodeto NFKC/NFD/NFC/NFKD/NFKC Casefold/Modified NFC/Modified NFDCotEditor 2.0.0+
normalize unicode selection of front document to NFC
move line upmove line downCotEditor 2.3.0+
sort linesreverse linesCotEditor 2.3.0+
delete duplicate lineCotEditor 2.3.0+