// Name: Auto Translate & Define // Description: Auto-detect language, translate, and provide dictionary definition with a keystroke // Author: dor-zarzilla // GitHub: dor-zarzilla import "@johnlindquist/kit" // Get selected text from the active application const selectedText = await getSelectedText() if (!selectedText || selectedText.trim() === '') { await div(md('# No Text Selected\n\nPlease select some text first, then run this script.')) exit() } const text = selectedText.trim() // Set up loading state setStatus({ message: "Detecting language and translating...", status: "busy" }) try { // Use AI to detect language and translate const translator = ai(` You are a language detection and translation expert. For the given text: 1. Detect the source language 2. If it's English, translate to Spanish 3. If it's not English, translate to English 4. Provide a brief dictionary definition or explanation of the key terms Respond in this exact format: **Source Language:** [language name] **Translation:** [translated text] **Definition:** [brief definition or explanation of key terms] `) const result = await translator(text) // Display results in a formatted div await div({ html: md(` # Translation & Definition **Original Text:** ${text} ${result} --- *Tip: Select text and run this script again for instant translation* `), css: ` body { font-family: system-ui, -apple-system, sans-serif; line-height: 1.6; padding: 20px; } strong { color: var(--color-primary); } `, shortcuts: [ { name: 'Copy Translation', key: `${cmd}+c`, onPress: () => { // Extract just the translation part const translationMatch = result.match(/\*\*Translation:\*\* (.+?)(?=\n|$)/i) if (translationMatch) { copy(translationMatch[1]) toast('Translation copied to clipboard!') } }, bar: 'right' }, { name: 'Paste Translation', key: `${cmd}+v`, onPress: async () => { const translationMatch = result.match(/\*\*Translation:\*\* (.+?)(?=\n|$)/i) if (translationMatch) { await setSelectedText(translationMatch[1]) toast('Translation pasted!') submit() } }, bar: 'right' } ] }) } catch (error) { await div(md(` # Translation Error Sorry, there was an error processing the translation: \`\`\` ${error.message} \`\`\` Please make sure you have your AI API key configured in your environment variables. `)) } finally { setStatus({ message: "", status: "ready" }) }