Working with UNIX scripts

This page explains how to pass your document's text to UNIX scripts that are executable via the Script menu, or how to receive the outputs from them.

Referring to the document file path

If the frontmost document has already been saved, the absolute file path of the document will be referred to as an argument.

Passing text data to a script

To pass text data from CotEditor to your script, you need to put a comment at the beginning of the script and write “%%%{CotEditorXInput=xxxx}%%%” inside. Replace “xxxx” with one of the parameters below.

SelectionTo pass the text you are currently selecting.
AllTextTo pass the whole text of your document.
NoneTo pass nothing (default).

Receiving output data from a script

To make CotEditor receive output data from a script, you need to put a comment at the beginning of the script and write “%%%{CotEditorXOutput=xxxx}%%%” inside. Replace “xxxx” with one of the parameters below.

ReplaceSelectionTo replace the current selection with the contents of the output data.
ReplaceAllTextTo replace the whole text of your document with the contents of the output data.
InsertAfterSelectionTo insert the contents of the output data right after the current selection.
AppendToAllTextTo insert the contents of the output data at the end of your document.
NewDocumentTo create a new document and insert the contents to it.
PasteboardputsTo store the contents of the output data in clipboard.
DiscardTo do nothing (default).

Example

The following Python script prepends “>” character to every line in the selection of the frontmost document.

#!/usr/bin/env python
# %%%{CotEditorXInput=Selection}%%%
# %%%{CotEditorXOutput=ReplaceSelection}%%%

import sys
for line in sys.stdin:
    print(">" + line.rstrip())

You can get more sample scripts on:
coteditor/SampleScripts -GitHub

See also