You can automate many tasks such as creating and editing documents, working with selections, and controlling windows using AppleScript or JavaScript for Automation (JXA). This page describes the scripting interface provided by CotEditor, focusing on the available classes and commands.
CotEditor extends the standard system scripting terms with its own classes and commands, allowing scripts to interact more directly with CotEditor documents and features. You can also use script hooks to automatically run scripts in response to specific events. For details, see Run script on specific events.
You can also view CotEditor’s scripting interface in Script Editor’s library. To open the scripting dictionary:
Go to the CotEditor app
on your Mac.
Choose Help > CotEditor Scripting Guide > AppleScript Dictionary.
For the version history of the AppleScript support on CotEditor, see Support different CotEditor versions in AppleScript.
class
documentA CotEditor document.
contents:
The contents of the document. (text)
text:
The contents of the document. (text)
editable:
Is the document editable? (boolean) new on CotEditor 5.1
deprecated on CotEditor 4.4length:
The number of characters of the document in UTF-16. (int)
selection:
The current selection. (text selection)
encoding:
The text encoding name of the document. (text)
IANA charset:
The IANA charset name of the document. (text, such as Shift_JIS or EUC-JP)
has BOM:
Has the text encoding a BOM (byte order mark)? (boolean) new on CotEditor 4.1
line ending:
The line ending code of the document. (CR / LF / CRLF / NEL / LS / PS)
tab width:
The width of a tab character in space equivalents. (int) new on CotEditor 2.1
expands tab:
Are tab characters expanded to space? (boolean) new on CotEditor 3.1.2
wrap lines:
Whether to wrap lines or not. (boolean)
coloring style:
The syntax name of the document. (text)
class
folder documentCotEditor folder document
current document:
The currently displayed document (document).
A document opened by opening a folder is treated as an instance of the folder document class. You can obtain the currently open document in that folder document via the current document property. In addition, any document API sent directly to a folder document is forwarded to its current document. If no current document exists, commands that target document will return an error.
The following two commands produce the same result:
if front document is folder document then
set theDocument to front document's current document
else
set theDocument to front document
end if
tell theDocument
set theText to contents of selection
end tell
tell front document
set theText to contents of selection
end tell
class
text selectionThe current selection.
contents:
The contents of the selection.
range:
The range of characters in the selection. The format is '{location, length}'.
line range:
The range of lines in the selection. The format is '{location, length}'. (length 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 from CotEditor 5.0, characters are counted in the Unicode grapheme cluster unit. This is the same as the specification of AppleScript 2.0.
If you specify a negative value for location, CotEditor counts from the end of the document. If the specified length exceeds the remaining characters, CotEditor selects text up to the end of the document. If 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 insertion point 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.
command
write to consolePrint text on CotEditor’s console window.
[title (boolean)]:
Should the script name appear with? (added on CotEditor 5.0.7)
[timestamp (boolean)]:
Should the timestamp appear with? (added on CotEditor 5.0.7)
write to console "Script failed."
write to console "calculating…" without title and timestamp
command
findSearches for text and selects the first match.
Returns true if a match is found; otherwise, returns false.
for (text):
The text to search for.
[RE (boolean)]:
Perform regular expression search or not.
[wrap (boolean)]:
Perform wrap search or not.
[ignore case (boolean)]:
Ignore case or not.
[backwards (boolean)]:
Perform backwards search or not.
document objectfind front document for "Apple" with ignore case
The search starts from the current selection (insertion point). For example, when not using the wrap or backwards options and when there are no matching text 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.
command
replaceSearches for text and replaces them with other text.
Returns the number of replacements, if any, otherwise returns 0.
for (text):
The text to search for.
to (text):
The text to replace with.
[all (boolean)]:
Search the whole document or not.
[RE (boolean)]:
Perform regular expression search or not.
[wrap (boolean)]:
Perform wrap search or not.
[ignore case (boolean)]:
Ignore case or not.
[backwards (boolean)]:
Perform backwards search or not.
document objectreplace 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 (insertion point). Use the all option for searching the whole document.
After replacing the whole document using the all option, the insertion point moves to the head of the document. If there were no matching text, the insertion point doesn’t move.
The regular expression search cannot search backwards. If both options were specified at the same time, the backwards option is ignored.
command
scroll to caretScrolls the window so that the insertion point or the selection can be seen.
document objectscroll to caret front document
command
jumpMove the insertion point to the specified location. At least one parameter is required.
to line (int):
The number of the line to jump.
[column (int)]:
The location in the line to jump.
jump front document to line -1
If a negative value is provided, the line/column is counted from the end.
command
convertConverts the text encoding of the document.
to (text):
The new encoding, either in localized encoding name or an IANA charset name.
[lossy] (boolean):
Whether to allow “lossy” conversion (might result in loss of character that couldn’t be converted) or not.
[BOM] (boolean):
Whether to add a BOM (byte order mark). (added on CotEditor 4.1)
document objectconvert 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.
command
reinterpretReinterpret the document with the specified text encoding.
as (text):
The encoding for reinterpreting, either in localized encoding name or an IANA charset name.
document objectreinterpret front document as "Japanese (EUC)"
Returns false if the file has never been saved.
Changes that aren’t yet saved are lost.
From CotEditor 4.0.7 on, the new encoding name accepts also IANA charset names.
command
shift leftShifts the current line left.
selection objectcommand
shift rightShifts the current line right.
selection objectshift right selection of front document
command
comment outAppend comment delimiters to selected text if possible.
selection objectcomment 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.
command
uncommentRemove comment delimiters from selected text if possible.
selection objectThese 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.
command
stringReturns the text in the specified range regardless of the current selection.
in (list):
The range.
selection objectstring front document in {0, 10}
Returns empty text if the specified range was invalid.
This command doesn’t change the specified range.
command
change caseUppercases/Lowercases/Capitalizes words.
to upper / lower / capitalized:
The case type to change.
selection objectchange case selection of front document to upper
command
change roman widthConverts between halfwidth and full-width characters.
to half / full:
The text width to change.
selection objectchange roman width selection of front document to full
command
smart quotesConvert straight quotes in the selected text to typographical quotes, if possible.
selection objectsmarten quotes selection of front document
command
straighten quotesConvert typographical quotes in the selected text to straight quotes, if possible.
selection objectcommand
smart dashesConvert double hyphens in the selected text to dashes, if possible.
selection objectsmarten dashes selection of front document
command
normalize unicodePerforms Unicode normalization.
to NFKC / NFD / NFC / NFKD / NFKC Casefold / Modified NFC / Modified NFD:
The normalized forms of Unicode text.
selection objectnormalize unicode selection of front document to NFC
command
move line upSwap selected lines with the line just above.
selection objectcommand
move line downSwap selected lines with the line just below.
selection objectcommand
sort linesSort selected lines ascending.
selection objectcommand
reverse linesReverse selected lines.
selection objectcommand
delete duplicate lineDelete duplicate lines in selection.
selection object