Skip to main content

Shortcuts Integration

Run macOS Shortcuts with your selected text.

How It Works

OnText can trigger any Shortcut from the Shortcuts app, passing the selected text as input.

Setting Up

  1. Create a Shortcut in the Shortcuts app
  2. In OnText, create a new action
  3. Select Shortcut as the action type
  4. Enter the exact name of your Shortcut

Examples

Create a Shortcut

  1. Open Shortcuts app
  2. Click + to create a new shortcut
  3. Add actions to process text input
  4. Where the selected text should go, set it to receive Shortcut Input
  5. Save with a memorable name

Example Shortcuts

Add to Notes:

  1. Shortcut receives text as input
  2. "Create Note" action with input as body

Translate Text:

  1. Shortcut receives text as input
  2. "Translate Text" action
  3. "Show Result" or "Copy to Clipboard"

Summarize with AI:

  1. Shortcut receives text as input
  2. "Use Model" action (for AI summarization)
  3. "Show Result" action

Passing Text to Shortcuts

OnText passes selected text as Shortcut Input. In your Shortcut:

  1. Use "Shortcut Input" variable to access the text
  2. Process it with any Shortcut actions
  3. Optionally return a result

Advanced: Getting Results Back

You can call Shortcuts from a Shell Script and use the returned value. Here's an example that uses a Shortcut for language detection:

#!/bin/zsh
API_KEY="your-api-key-here"
API_URL="https://api-free.deepl.com/v2/translate"

INPUT_TEXT='{text}'

# Detect language using Shortcuts
DETECTED_LANG=$(osascript -e "tell application \"Shortcuts\" to run shortcut \"detect lang\" with input \"$INPUT_TEXT\"" 2>/dev/null)

# Translate based on detected language
if [[ "$DETECTED_LANG" == "Korean" ]] || [[ "$DETECTED_LANG" == "ko" ]]; then
TARGET_LANG="EN-US"
else
TARGET_LANG="KO"
fi

# Call translation API
RESULT=$(/usr/bin/python3 -c "
import sys, json, urllib.request

text = '''$INPUT_TEXT'''
api_key = '$API_KEY'
url = '$API_URL'
target_lang = '$TARGET_LANG'

data = {'text': [text], 'target_lang': target_lang}
headers = {'Authorization': f'DeepL-Auth-Key {api_key}', 'Content-Type': 'application/json'}

try:
req = urllib.request.Request(url, data=json.dumps(data).encode('utf-8'), headers=headers)
with urllib.request.urlopen(req) as response:
print(json.loads(response.read().decode('utf-8'))['translations'][0]['text'], end='')
except Exception as e:
print(f'Error: {e}', end='')
" 2>&1)

echo -n "$RESULT" | pbcopy
sleep 0.1
osascript -e 'tell application "System Events" to keystroke "v" using command down'
Pro Tip

This pattern lets you leverage Shortcuts for tasks that are easier to configure visually (like language detection, text recognition, etc.) while still using Shell Scripts for complex processing!

Limitations

  • Shortcut must exist in your Shortcuts app
  • Name must match exactly (case-sensitive)
  • Complex Shortcuts may take longer to execute