Created
November 2, 2016 17:37
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |
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
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
``