Technical Context
macOS currently lacks a built-in, free on-the-spot text translation feature. Translating Chinese to English typically involves multiple mouse clicks and navigations, which disrupts workflow.
Chrome’s Immersive Translate extension addresses part of this, but it has limitations:
- Works only within the Chrome browser
- Translates entire input box content instead of selected segments
- Uses triple-space trigger, which feels awkward and less efficient than dedicated shortcuts
Approach
Build an automation script with these core components:
- Text Capture: Simulate Cmd+C to copy selected text
- Translation: Use macOS Shortcuts to wrap a translation API call
- Text Insertoin: Simulate Cmd+V to paste the translated result
Step 1: Add the macOS Shortcut
Use this iCloud link to install a preconfigured "Translate to English" shortcut directly: https://www.icloud.com/shortcuts/f02cd8ebbf1d46338cfaee2fdbbd4734
Step 2: Set Up Hammerspoon
Hammerspoon is a free, open-source automation tool for macOS. Install it from the official website, then grant it accessibility permissions via System Settings > Privacy & Security > Accessibility.
Step 3: Configure Hammerspoon Script
Launch Hammerspoon, click the menu bar icon, and select "Open Config". This opens the init.lua conifguration file. Replace or append the following script:
-- Core translation logic
function performTranslation()
-- Capture selected text
hs.eventtap.keyStroke({"cmd"}, "c")
-- Call the macOS Shortcut to translate
local translatedText, isSuccess, _, _ = hs.execute('shortcuts run "Translate to English"', true)
-- Validate and process the result
if translatedText then
-- Clear clipboard and set to translated content
hs.pasteboard.clearContents()
hs.pasteboard.setContents(translatedText)
-- Insert the translated text
hs.eventtap.keyStroke({"cmd"}, "v")
end
end
-- Global shortcut bindings
-- Option+Shift+I: Translate selected text
hs.hotkey.bind({"option", "shift"}, "i", performTranslation)
-- Option+Shift+O: Auto-select all text and translate
hs.hotkey.bind({"option", "shift"}, "o", function()
hs.eventtap.keyStroke({"cmd"}, "a")
performTranslation()
end)
Step 4: Apply the Script
After pasting the code, save init.lua, then reload the Hammerspoon configuration:
- Click the Hammerspoon menu bar icon
- Select "Reload Config"
Usage
- Option+Shift+I: Translates only the currently selected text
- Option+Shift+O: Automatically selects all text in the active input field and translates it