Skip to content

Instantly share code, notes, and snippets.

@roelven
Last active May 1, 2025 14:48
Show Gist options
  • Save roelven/73eb3b56ab7ba8499f8d7411bd36c9a7 to your computer and use it in GitHub Desktop.
Save roelven/73eb3b56ab7ba8499f8d7411bd36c9a7 to your computer and use it in GitHub Desktop.
Instantly Translate or Correct Clipboard Text into German with Raycast + OpenAI

Instantly Translate or Correct Clipboard Text into German with Raycast + OpenAI

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

πŸ“‹ Prerequisites

  • MacOS with Raycast
  • OpenAI API key (platform.openai.com)
  • jq installed for parsing JSON:

brew install jq

βš™οΈ Setup

  1. Create new file in your Raycast Script Commands folder (e.g., ~/.raycast/scripts/translate_to_german.sh).

  2. Paste the script below into the file.

  3. Make the script executable: chmod +x ~/.raycast/scripts/translate_to_german.sh

  4. Replace YOUR_OPENAI_API_KEY_HERE with your actual OpenAI API key.

  5. Reload Raycast script directories

  6. Set a keyboard shortcut to trigger it even faster (I use βŒ₯ + ⇧ + G)

πŸš€ The Script


#!/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 β˜‘οΈ"

πŸ’‘ Notes

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment