Google translate client for OS X

Open /Applications/Automator.app, select to create a new Service, double-click Run AppleScript from the Utilities library, and enter the following script code into the text field:

on run argv
    tell application "Safari"
        make new document at end of documents
        set URL of document 1 to "https://translate.google.com/#view=home&op=translate&sl=en&tl=es&text=" & item 1 of argv
    end tell
end run

Save as Translate to Spanish.


Now you can select text in any application, and select Translate to Spanish from the context menu, or the Application » Services menu. A new Safari window will open, with the selected text as input to Google Translate.


You can assign a keyboard shortcut in System Preferences » Keyboard » Keyboard Shortcuts » Services.


Selecting from context menu (it's a submenu since I have too many applicable services, you can disable some in System Preferences):

enter image description here


The following page opens after clicking the menu item:

enter image description here


I'd prefer a native application or a ⌃⌘D-style panel as well. But for now I'm using this AppleScript:

try
    tell application (path to frontmost application as text)
        set ans to text returned of (display dialog "" default answer "ja ")
    end tell

    set offs to offset of space in ans
    set i1 to text 1 thru (offs - 1) of ans
    set i2 to text (offs + 1) thru -1 of ans

    set sl to "en"
    set tl to "en"
    set z to offset of "-" in i1
    if i1 is "-" then
        set sl to "auto"
    else if z is 0 then
        set tl to i1
    else if z is (count i1) then
        set sl to text 1 thru -2 of i1
    else
        set sl to text 1 thru (z - 1) of i1
        set tl to text (z + 1) thru -1 of i1
    end if
    set base to "http://translate.google.com/#"
    set u to base & sl & "|" & tl & "|" & urldecode(i2)

    tell application "Safari"
        activate
        open location u
    end tell
end try

on urldecode(x)
    set cmd to "'require \"cgi\"; puts CGI.escape(STDIN.read.chomp)'"
    do shell script "echo " & quoted form of x & " | ruby -e " & cmd
end urldecode

The web client has some features that are essential to me, like transliterating text to the latin alphabet from other writing systems, and providing alternative translations for single words.

Extra: minimal userstyle for Google Translate.


Open up Automator
Select Service
Select Utilities under Library
Select Run Shell Script
On the 'Shell:' dropdown menu, select '/usr/bin/ruby'
Type into the textbox:

require 'cgi'<br>
`open 'http://translate.google.com/#auto/en/#{CGI.escape(STDIN.read.chomp)}'`

Save the script as 'Translate to English' or whatever

Now, right clicking on any highlighted text and selecting 'Translate to English' will open up a new Google Translate page with the highlighted text translated into English.