Skip to main content

Run a Shell Script on Selected Text on Mac with OnText

· 3 min read
gityeop
Creator of OnText

OnText lets you run a shell script on selected text on your Mac. Save the command as an action, select text in a supported app, and trigger it from the OnText panel. A repeated trip through Terminal becomes an action you can reuse while you work.

This walkthrough creates an uppercase action with a visible result, then shows how to paste that result back. Shell Script actions require OnText Pro. Updated October 24, 2026.

Create Your First Selected-Text Shell Action​

  1. Open OnText Preferences → Actions and click +.
  2. Set the title to Uppercase and choose Shell Script as the type.
  3. Paste the command below into the command field.
  4. Assign an available action key if you want to run it from the keyboard.
printf '%s' "$ONTEXT_TEXT" | tr 'a-z' 'A-Z' | pbcopy

OnText runs shell commands with /bin/zsh. The selected text arrives in the ONTEXT_TEXT environment variable. This example uses the Mac's built-in tr and pbcopy commands to uppercase English ASCII letters and copy the result.

Try It With a Concrete Input​

Type this in an editable document, then select it:

ship the next update

Press F2 or your configured trigger shortcut, then run Uppercase. Paste into the document to check the clipboard result:

SHIP THE NEXT UPDATE

The original selection stays unchanged until you paste. Once the action is configured, you can repeat the same selection → trigger → action rhythm with another phrase.

Paste the Result Back Into the Selection​

To replace the manual paste step, use this command instead:

printf '%s' "$ONTEXT_TEXT" | tr 'a-z' 'A-Z' | pbcopy && {paste}

OnText's {paste} placeholder pastes the clipboard result back into the active app. Try it in an editable text field: select ship the next update, run the action, and the selected text becomes SHIP THE NEXT UPDATE. A read-only page can be a source for text capture, but you need an editable destination to replace text.

This is the kind of small automation OnText is built for: configure a useful command once, then keep using it close to your text. The command's own processing time still depends on what it does.

Keep Selected Text Separate From the Command​

Use "$ONTEXT_TEXT" with double quotes. That passes spaces, quotation marks, and other characters as text rather than inserting them into shell source. Use printf '%s' to preserve the input value.

Input you needShell value
Selected text"$ONTEXT_TEXT"
Selection captured through Copy"$ONTEXT_TEXT_WITH_COPY"
Clipboard text"$ONTEXT_CLIPBOARD"
First answer to a declared prompt"$ONTEXT_PROMPT_1"

Do not insert {text}, {clipboard}, or prompt input directly into shell command source. See the placeholders reference for prompt declarations and the other supported inputs.

Choose the Right Tool for the Next Action​

Use a shell action when a command-line tool already does the transformation you need. For example, you can trim text, format an identifier, or pass a selection to an existing local script.

Use a URL action for a search or app link, AppleScript for app scripting, and a Shortcut action for an automation you maintain visually in Apple's Shortcuts app. Regex context rules control when an action appears for matching text; they do not replace the script that transforms it.

Read Shell Scripts for more commands, or Custom Actions to configure action keys and context rules. New to OnText? Start with First Steps.