5

Having recently moved to Germany, I receive tons of German emails.

My current workflow is definitely not efficient: Copy mail contents, goto translate.google.com, paste content..

The ideal solution would be: just trigger a shortcut and have the contents of the email translated into the predefined language you've set.

Would that be possible?

3 Answers 3

5

Here is an applescript I just put together that is working for me (10.9)

You can place it in the Applescript menu and run it on the selected Emails.

The script will:

  • Try and grab the select emails in Mail.
  • Send a Request to google translate
  • Convert the resulting HTML to plain text
  • Display a temporary text file with the translation.

You can set the original language to auto or one of the languages in the languages_code list. And also set the result language from the list.

There is no reason you should not be able to create a service using this script. But I will leave that to you. Just note that for the device to work you will need to right click/crtl +click in a message's body for the service menu to show up. Or use the Services menu.

#http://macscripter.net/viewtopic.php?id=31218
#http://macscripter.net/viewtopic.php?id=39742
#The above links are the starting points of this script which both attempt to translate text. But are broken or do not return the full text.

#This script will attampt to translate the text from the selected emails to your chosen language 

property languages_code : {Afrikaans:"af", Albanian:"sq", Arabic:"ar", Belarusian:"be", Bulgarian:"bg", Catalan:"ca", Chinese:"zh-CN", Croatian:"hr", Czech:"cs", Danish:"da", Dutch:"nl", English:"en", Estonian:"et", Filipino:"tl", Finnish:"fi", French:"fr", Galician:"gl", German:"de", Greek:"el", Hebrew:"iw", Hindi:"hi", Hungarian:"hu", Icelandic:"is", Indonesian:"id", Irish:"ga", Italian:"it", Japanese:"ja", Korean:"ko", Latvian:"lv", Lithuanian:"lt", Macedonian:"mk", Malay:"ms", Maltese:"mt", Norwegian:"no", Persian:"fa", Polish:"pl", Portuguese:"pt", Romanian:"ro", Russian:"ru", Serbian:"sr", Slovak:"sk", Slovenian:"sl", Spanish:"es", Swahili:"sw", Swedish:"sv", Thai:"th", Turkish:"tr", Ukrainian:"uk", Vietnamese:"vi", Welsh:"cy", Yiddish:"yi"}

#Curl stuff
property agent : "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_2; en-us) AppleWebKit/531.21.8 (KHTML, like Gecko) Version/4.0.4 Safari/531.21.10"
property charset : "UTF-8"
property header : "Charset:" & space & charset
property theURL : "https://translate.google.com/?" #This must be https

#used to delimit the html
property snipOpen : "TRANSLATED_TEXT="
property snipClose : ";INPUT_TOOL_PATH"

#You can either set a start language or leave it at auto.
property startLang : "auto"
#set a result language 
property resultLang : English of languages_code

#Ask Mail to get and process selected emails
tell application "Mail"
    set theMessages to (get selection)
    repeat with i from 1 to number of items in theMessages
        set this_item to item i of theMessages

        set thisSender to sender of this_item
        set thisSubject to subject of this_item
        set thisText to content of this_item as string
        my translate(thisSender, thisSubject, thisText)
    end repeat
end tell

(*************** SUBROUTINES *****************)
#Translate the message text and display them in a temp text file
on translate(thisSender, thisSubject, thisText)

    if thisText is not "" then
        #replace all spaces with "%20" for the URL to use without error
        set escapedText to findReplace(thisText, space, "%20")

        #send request  to google and get the returned HTML
        set TranslatedText to do shell script "/usr/bin/curl" & space & ¬
            "-A" & space & quoted form of agent & space & ¬
            "-H" & space & quoted form of header & space & ¬
            "-d" & space & quoted form of ("&ie=" & charset & "&oe=" & charset & "&langpair=" & startLang & "|" & resultLang & "&text=" & escapedText) & space & quoted form of theURL


        try
            #Use delimiters to split the text to just get the actual result part 
            set txt to Split(TranslatedText, snipOpen)'s item 2
            set txt to Split(txt, snipClose)'s item 1

            set displayText to ¬
                "Sender: " & thisSender & "<br>" & ¬
                "Subject: " & thisSubject & "<br>" & "<br>" & ¬
                txt
            #Use  Textutil to strip any other rich text or HTML code out and convert to plain text. Then open in a text document
            do shell script "echo  " & quoted form of displayText & "|textutil -format html -convert txt -stdin -stdout | open -f"
        on error errTxt number errNum
            display dialog errTxt with title "Error# " & errNum buttons {"Cancel", "OK"} default button 2 with icon 0 giving up after 0
        end try

    end if
end translate


on Split(txt, del)
    set {otid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, del}
    set txt to text items of txt
    set AppleScript's text item delimiters to otid
    return txt
end Split



on findReplace(theString, search_string, replacement_string)
    if theString contains search_string then
        set AppleScript's text item delimiters to search_string
        set text_item_list to text items of theString
        set AppleScript's text item delimiters to replacement_string
        set theString to text_item_list as text
        set AppleScript's text item delimiters to ""
    end if
    return theString
end findReplace
3
  • This script is great, thanks. I can translate my emails by selecting in the mailbox list, and then running in the AppleScript editor. I'm new to this kind of customization however. What do you mean by "add to the AppleScript menu"? Mar 24, 2014 at 11:24
  • There is an explanation here cbreier.com/2013/02/… which save me writing it all up :-)
    – markhunte
    Mar 24, 2014 at 13:00
  • I think the video can be summarized as 1) Enable "Show Script Menu" in AppleScript Editor preferences; then 2) Create the above file in /Library/Scripts/ May 2, 2014 at 8:13
0

You can do this using the provided Apple application 'Automator' (you'll find it in the Applications folder) which allows you to do drag and drop type automation scripts. You could then assign the Automator action to a dedicated key combination.

You can even record the steps in Automator then clean them up to make it generic.

Automator and Applescript are two features of OS X that are very little known about but can help in exactly the sort of case you are looking at.

Website with info: http://www.macosxautomation.com/automator/

And creating services: http://www.automatedworkflows.com/2011/05/22/creating-applescript-services-in-mac-os-x/

6
  • 2
    This should really be a comment because it is not really an answer
    – markhunte
    Oct 27, 2013 at 11:06
  • According to you... the question was "would that be possible" the answer I gave was "yes it is". So the question was answered. It's good that you, obviously having lots of time, wrote a script for the OP and kudos to you. But that does not change the fact that the original question was answered. That is, of course, unless there is a requirement on Ask Different that responders are expected to write code for people also.
    – FromOZ
    Oct 27, 2013 at 16:17
  • It does not help that the question is not asked very well. The reason I suggested it should have been a comment is because as you yourself summed up your answer : 'yes it is'. Which I feel should be a comment. This site allows for that opinion. This is something that once read I wanted to have myself. It took a tiny bit of my time to write the code. And was worth paying it forward. I have not suggested you should have done the same. Nor have I said what you have written is wrong it is not. Just my opinion that your answer is not that helpful as an answer. But is as a comment
    – markhunte
    Oct 27, 2013 at 17:43
  • Again that is all your opinion. And as to whether "the question is not asked very well" well that also is debatable. The OP didn't ask for someone to write the code for him, he asked was it possible, the unspoken word being "somehow". I answered his question and pointed him in the direction of the technologies to use. Therefore question answered. You wanted to go further spoon fed him the answer by writing the specific code - great, no problem. But that doesn't mean that you should characterise a valid answer to the original question as a comment. And let's leave it at that.
    – FromOZ
    Oct 27, 2013 at 21:19
  • As I said. I wrote the code for me and felt I should share it. The point of someone marking down and answer is how useful that individual deems it. Thats using their opinion. There is no getting away from that fact. Adding a comment as to why is sometimes useful. I could have made it more clear of why I did not think it was useful. Or I could have not left a comment at all leaving you wondering. In saying that, the comments are not here to rant at each other or have a pop a at each other. So if you want to go on further with this then please do. But I will not answer any more of them.
    – markhunte
    Oct 27, 2013 at 22:45
0

Actually, I found this more convenient. Tested on El captain 10.11.5

Is it possible to bind my Mac OSX Dictionary.app to Google Translate?

Screenshot: http://puu.sh/pjWSz/6d26d3b1cd.png

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .