# Script CotEditor on Mac with UNIX script

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

- The file extensions of the scripts you can add to the Script menu are .sh, .pl, .php, .rb, .py, .js, .awk, and .swift (regardless of the language)
- To run a script, you need to set execute permission for the script file.

## Refer to the document file path

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

## Pass text data to a script

A script can receive the contents of the frontmost document on STDIN (standard in). 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. With no parameters, CotEditor passes nothing just like when you put “None.”

- CotEditor passes text data encoded with UTF-8.

Keyword | Description
--- | ---
`Selection` | To pass the text you are currently selecting.
`AllText` | To pass the whole text of your document.
`None` | To pass nothing (default).

## Receive output data from a script

CotEditor can receive the output text from the script on STDOUT (standard out) and apply it to the document and so on. 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. Without parameters, CotEditor does nothing just like when you put “`Discard`.”

- The text that CotEditor receives must be encoded in UTF-8.

Keyword | Description
--- | ---
`ReplaceSelection` | To replace the current selection with the contents of the output data.
`ReplaceAllText` | To replace the whole text of your document with the contents of the output data.
`InsertAfterSelection` | To insert the contents of the output data right after the current selection.
`AppendToAllText` | To insert the contents of the output data at the end of your document.
`NewDocument` | To create a new document and insert the contents to it.
`Pasteboardputs` | To store the contents of the output data in the Clipboard.
`Discard` | To do nothing (default).

## Print text to the Console

The strings thrown to the standard error are printed in the Console window.

## Example

The following Python script prepends the “>” character to every line in the selection of the frontmost document, and then prints the number of the processed lines to the Console.

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

    import sys

    count = 0
    for line in sys.stdin:
        count += 1
        print(">" + line.rstrip())
    sys.stderr.write("Processed {} lines.".format(count))

Output to the Console

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

## See also

- Script CotEditor