Keywords: Script Menu, script

# 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.

- The file extensions of the scripts you can add to the Script menu are: .sh .pl .php .rb .py (regardless of their languages)
- In order to run a script, you need to set execute permission for the script file. To do so, use Terminal.app (located in Applications/Utilities) or chmod-755.app which is bundled in the Extras folder.
- Line endings in your script must be LF.

## 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.

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

- With no parameters, CotEditor passes nothing just like when you put “None.”
- A script should capture CotEditor's text data on STDIN (standard in).
- CotEditor passes text data with UTF-8 encoding.

## 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.

`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.
`Pasteboardputs` | To store the contents of the output data in clipboard.
`Discard` | To do nothing (default).

- With no parameters, CotEditor does nothing just like when you put “Discard.”
- A script should output the text data for CotEditor on STDOUT (standard out).
- The character encoding of the text that CotEditor receives must be UTF-8.

## See also

- Customizing the Script menu
- Working with AppleScript scripts
- File naming rules for CotEditor scripts