Shell Scripts
Run shell commands and scripts with your selected text.
Basic Shell Script
Shell script actions execute commands with /bin/zsh. Selected text is available as
the ONTEXT_TEXT environment variable.
printf '%s' "$ONTEXT_TEXT" | tr 'a-z' 'A-Z'
This converts selected text to uppercase.
Passing User Input Safely
User-controlled values are passed separately from the command source:
| Value | Environment variable |
|---|---|
| Selected text | ONTEXT_TEXT |
| Selected text captured with Cmd+C | ONTEXT_TEXT_WITH_COPY |
| Clipboard snapshot | ONTEXT_CLIPBOARD |
| Prompt input | ONTEXT_PROMPT_1, ONTEXT_PROMPT_2, ... |
Always put the variable expansion in double quotes, such as "$ONTEXT_TEXT".
Do not insert {text}, {clipboard}, or {prompt} directly into a Shell command.
OnText offers to migrate older actions when they run.
ONTEXT_CLIPBOARD follows a text-first policy: when a clipboard item contains
both text and image or rich-content formats, OnText uses the text representation.
The action stops only when the clipboard has no text representation.
See Placeholders for the action-specific syntax.
Common Patterns
Transform and copy to clipboard:
printf '%s' "$ONTEXT_TEXT" | tr 'a-z' 'A-Z' | pbcopy
Transform and paste back:
printf '%s' "$ONTEXT_TEXT" | tr 'a-z' 'A-Z' | pbcopy && {paste}
Instead of using osascript -e 'tell application "System Events" to keystroke "v" using command down', you can simply use {paste} placeholder for a cleaner script!
Examples
Text Transformation
Uppercase:
printf '%s' "$ONTEXT_TEXT" | tr 'a-z' 'A-Z' | pbcopy && {paste}
Lowercase:
printf '%s' "$ONTEXT_TEXT" | tr 'A-Z' 'a-z' | pbcopy && {paste}
Reverse text:
printf '%s' "$ONTEXT_TEXT" | rev | pbcopy && {paste}
Remove whitespace:
printf '%s' "$ONTEXT_TEXT" | tr -d ' ' | pbcopy && {paste}
Encoding
Base64 encode:
printf '%s' "$ONTEXT_TEXT" | base64 | pbcopy && {paste}
URL encode:
python3 -c 'import os, urllib.parse; print(urllib.parse.quote(os.environ["ONTEXT_TEXT"]))' | pbcopy && {paste}
Developer Tools
Format JSON:
printf '%s' "$ONTEXT_TEXT" | python3 -m json.tool | pbcopy
Count words:
printf '%s' "$ONTEXT_TEXT" | wc -w | tr -d ' '
Migrating Older Shell Actions
When an older Shell action inserts a user-content placeholder directly into the
command, OnText shows a review dialog before running it. The dialog displays the
original command and the version that uses quoted ONTEXT_* environment variables.
Choosing Migrate & Run saves the migrated action and then runs it. Choosing
Cancel leaves the action unchanged and does not run it.
OnText does not guess how to rewrite legacy placeholders inside command substitution
($()), backticks, ANSI-C quoted strings ($'...'), or heredocs. In those cases the
action is not run, and OnText asks you to replace the placeholder manually with the
appropriate quoted environment variable.
Tips
Prefer printf '%s' over echo. printf does not interpret text that begins with
options or contains backslash sequences.
For complex scripts, consider creating a shell script file and calling it:
/path/to/your/script.sh "$ONTEXT_TEXT"