Skip to content

Instantly share code, notes, and snippets.

@parano
Last active May 10, 2023 18:41
Show Gist options
  • Save parano/0b355250b5f13bdc656db523bbf8e23e to your computer and use it in GitHub Desktop.
Save parano/0b355250b5f13bdc656db523bbf8e23e to your computer and use it in GitHub Desktop.
Mac OS Quick Action: Lookup selected word in Google Translate
on run {input, parameters}
set phrase to input as string
set ui_lang to "en"
set from_lang to "auto"
set to_lang to "zh-CN"
set google_translate_url to "https://translate.google.com"
set google_translate_look_up_url to google_translate_url & "/?hl=" & ui_lang & "&sl=" & from_lang & "&tl=" & to_lang & "&text=" & phrase
tell application "Google Chrome"
repeat with chrome_window in windows
repeat with t in tabs of chrome_window
if URL of t starts with google_translate_url then
set URL of t to google_translate_look_up_url
return
end if
end repeat
end repeat
open location google_translate_look_up_url
activate
end tell
end run

Usage:

  1. Open Automator
  2. File > New > Choose "Quick Action"
  3. In the left sidebar, find "Library" > "Utilities" > "Run AppleScript" > double click it
  4. Copy & Paste the code above to the code section in the "Run AppleScript" panel you just added
  5. "Command + S" to save, give it an action name, such as "Lookup a word in Google"
  6. System Preference > Keyboard > Shortcuts, select "Services" in the list, and find "Lookup a word in Google"
  7. Click to add a keyboard shortcut for this action, e.g. Command + Shift + 1
  8. Now select a word with mouse and press the keyboard shortcut, it should open a Google Chrome tab with the google translate page open
@dontait
Copy link

dontait commented Feb 22, 2021

The selected text doesn't get copied into in Google translate for me - I have to command-C and then paste. Is there a way to make the script copy directly? I'm on Big Sur.

Solution
In Automator, at the top of the window choose 'text' in the dropdown menu to get:
'Workflow receives current text in any application'

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