11/12/14 19:00:12

Google Translate Selected Text OS X Service

Not entirely certain where I got this one from. Maybe macosxautomation, maybe somewhere else. No matter where I got it from, this System Service is quite handy when you tend to receive emails in various languages, that you do not speak yourself.

It takes selected text and uses the “auto” detection of Translate to translate to English.

on run {input, parameters}
    -- the following 2 lines should be on one line
    set output to "http://translate.google.com/translate_t?text=" & urldecode(input as string) & "&langpair=auto%7Cauto"
    -- above 2 lines should be on one line
    return output
end run

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