Skip to content

Instantly share code, notes, and snippets.

@peterwooley
Created November 2, 2016 17:37
Show Gist options
  • Save peterwooley/5b7eb9d66d53103ae464f380ea4fb652 to your computer and use it in GitHub Desktop.
Save peterwooley/5b7eb9d66d53103ae464f380ea4fb652 to your computer and use it in GitHub Desktop.
Run this AppleScript in an Automator service to open selected text in Google Translate.
-- Modified from http://scriptingosx.com/2016/01/build-a-google-translate-service/
on run {input, parameters}
set theText to input as string
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {return & linefeed, return, linefeed, "
", character id 8233, character id 8232}
-- list of line break chars from http://stackoverflow.com/questions/12546253/how-to-replace-n-in-applescript#12546965
set splitText to text items of theText
set AppleScript's text item delimiters to {space}
set theText to splitText as text
set AppleScript's text item delimiters to oldDelims
tell application "Safari"
activate
set d to make new document with properties {URL:"https://translate.google.com/#auto/en/" & theText}
end tell
end run
@applesean
Copy link

applesean commented Jan 15, 2021

This seems to work if I select text in an application that is not Safari, but if I select text within Safari, it doesn't seem to work. I'm running Mac OS 11.1 Big Sur (Safari 14.0.2). Any thoughts about what to change to get this to work?

@peterwooley
Copy link
Author

@applesean I'm running macOS 11.2 Beta 2 (20D5042d), so things may be a little different but: I used to be able to get this working by putting it in a new Automator Quick Action workflow but in order to get it to work in any app, I had to change the "Workflow receives current" option at the very top of the automator workflow to "text" instead of "Automatic". Since I changed it, I'v been able to auto-translate from any app including Safari. If that's not how yours is set to "text", would you give it a try?

If you tried it and it's still not working, can you tell me if it opens the Safari window to Google Translate and just shows nothing or if it doesn't open the Safari window at all?

@applesean
Copy link

So I was selecting text in the "Workflow receives current" menu. Actually, if you don't, then the Service doesn't show up in the Service menu. Strangely, that did not fix the problem – still a blank webpage when the workflow runs that opens on top of the Safari window that had the text.

I did hobble together an alternative script that is a bit short, and I'll paste here just for future reference, should someone need it. I still would love to know why this script doesn't load the Google Translate webpage in Safari, especially when the syntax checks out.

The simplified AppleScript for the QuickAction workflow in Automator that I've implemented as an alternative:
``

on run {input, parameters}
set phrase to input as string
set myBrowser to "Safari"
set toLang to "en"
tell application myBrowser
open location "https://translate.google.com/#auto/" & toLang & "/" & phrase
end tell
end run
``
Screen Shot 2021-01-20 at 01 30 40

@woodjmichael
Copy link

No issue in macOS 11.3.1 with both the suggested apple scripts and the "Workflow receives current: text" option. Just make sure you create a Quick Action not a Workflow.

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