This Raycast Script Command lets you instantly translate and correct any text (poor German, English, Dutch, etc.) into grammatically correct German β triggered by a shortcut on macOS.
The script:
- Reads the current clipboard text
- Checks if the clipboard contents don't look like a password
- Sends it to OpenAI's GPT-4 Turbo model
- Receives corrected German
- Copies the result back to your clipboard
- Displays a small macOS notification when done
- MacOS with Raycast
- OpenAI API key (platform.openai.com)
- jq installed for parsing JSON:
brew install jq
-
Create new file in your Raycast Script Commands folder (e.g.,
~/.raycast/scripts/translate_to_german.sh
). -
Paste the script below into the file.
-
Make the script executable:
chmod +x ~/.raycast/scripts/translate_to_german.sh
-
Replace
YOUR_OPENAI_API_KEY_HERE
with your actual OpenAI API key. -
Reload Raycast script directories
-
Set a keyboard shortcut to trigger it even faster (I use
β₯ + β§ + G
)
#!/bin/bash
# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title Translate to German
# @raycast.mode silent
# Optional parameters:
# @raycast.icon π
# @raycast.packageName Language Tools
# Documentation:
# @raycast.description Translates clipboard text into grammatically correct German using GPT-4 and copies it back to clipboard.
# @raycast.author Roel van der Ven
# @raycast.authorURL https://roelvanderven.com
# CONFIGURATION
OPENAI_API_KEY="XXXXXXX"
LOGGING_ENABLED=false
LOGFILE="/tmp/translate_log.txt"
# Read clipboard
INPUT=$(pbpaste)
# Validate input: skip if one word or hash-like
if [[ "$INPUT" =~ ^[[:alnum:]]+$ ]]; then
echo "β Skipped: single word detected"
exit 0
fi
if [[ "$INPUT" =~ ^[a-f0-9]{4,}-[a-f0-9]{4,}-[a-f0-9]{4,}$ ]]; then
echo "β Skipped: hash-like pattern detected"
exit 0
fi
if [ "$LOGGING_ENABLED" = true ]; then
echo "--- $(date) ---" >> $LOGFILE
echo "Clipboard:" >> $LOGFILE
echo "$INPUT" >> $LOGFILE
fi
# Escape input safely for JSON
ESCAPED_INPUT=$(echo "$INPUT" | jq -Rs .)
# API request
RESPONSE=$(curl -s https://api.openai.com/v1/chat/completions \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d "{
\"model\": \"gpt-4-turbo\",
\"messages\": [
{\"role\": \"system\", \"content\": \"Translate the text into grammatically correct German. Only output the corrected German text. No comments.\"},
{\"role\": \"user\", \"content\": $ESCAPED_INPUT}
],
\"temperature\": 0.2
}")
if [ "$LOGGING_ENABLED" = true ]; then
echo "Response:" >> $LOGFILE
echo "$RESPONSE" >> $LOGFILE
fi
# Check for API error
if echo "$RESPONSE" | grep -q '"error"'; then
ERROR_MSG=$(echo "$RESPONSE" | jq -r '.error.message')
if [ "$LOGGING_ENABLED" = true ]; then
echo "API error: $ERROR_MSG" >> $LOGFILE
fi
echo "β οΈ Translation failed. \"API Error: $ERROR_MSG\" with title \"OpenAI Error\""
exit 1
fi
# Extract translation
TRANSLATED=$(echo "$RESPONSE" | jq -r '.choices[0].message.content')
if [ "$LOGGING_ENABLED" = true ]; then
echo "Parsed result:" >> $LOGFILE
echo "$TRANSLATED" >> $LOGFILE
fi
# Handle null output
if [ "$TRANSLATED" == "null" ]; then
echo "Translation output was null"
exit 1
fi
# Copy to clipboard and notify
echo "$TRANSLATED" | pbcopy
echo "Translated! Corrected German copied βοΈ"
If you want the output displayed inside Raycast instead of just copying to clipboard, you can modify the script to output the translated text directly instead of staying "silent." You can easily adapt this script to use your local LLMs (e.g., LibreChat or Ollama) if you rather want this to work without using the cloud.