0

I receive many emails in Japanese everyday (I am living in Japan). Some are spam but some are important. I would like to have an AppleScript to do the following

  1. Translate the email (with google translate for example)
  2. Forward back the translated email to my own email account

In this way, I will save the time to translate every email every day to understand which one is important and which one is not...

I think this can be done with AppleScript. I can make a rule in Mail to run an AppleScript every time that I receive a message from a certain mail account. I checked and it worked. But the problem is I am not able to write the AppleScript. I am not familiar with AppleScript, and honestly, I don't know where to start!

Edit: Alternatively, the translation can be also done with another software, which can be installed locally. Free or non free software does not matter.

8
  • Please edit your question to add your rule so we can see what you're doing. Also, include any error messages you've received.
    – fsb
    Jun 1, 2020 at 12:42
  • @fsb Thank you for the comment. I think I understand the workflow and I am pretty sure it can be done with AppleScript...but I am not familiar with AppleScript...can anybody help?
    – sintetico
    Jun 2, 2020 at 4:14
  • 1
    Hi sintetico, this is an interesting request. My thoughts: on the one hand, you can't use Google Translate (or other product like Bing Microsoft Translator) for this: 1) the number of words that can be translated at a time are limited and 2) it is tricky to extract the translated text out of a web interface. Google and Microsoft offer professional solutions that support an easy-to-use (from the programmer's point of view) REST interface and don't have the restrictions mentioned. Those services are not free, though. (Continues in next comment.)
    – jaume
    Jun 9, 2020 at 11:09
  • 1
    On the other hand, AppleScript would need a script/program to do the heavy lifting (create a request with the JSON-encoded contents of the mail message, get the translated result, paste it into a new message and send it back to you). That's a LOT of work. Wouldn't it be easier to switch to Gmail (of forward your messages to Gmail) and use Google Translate there? (see lifewire.com/… for details).
    – jaume
    Jun 9, 2020 at 11:09
  • @jaume Thank you for the comment. I did not imagine such complications. So, it is not a problem if I need to buy some software. About the gmail solution: I would like to use the mail program on the Mac because I am used to it and I do not like to use a web interface for emails. Also the mail.app integrates with the other desktop apps and this is a big plus. The problem is that I cannot redirect the translated emails from gmail and read the translated version in the mail.app
    – sintetico
    Jun 9, 2020 at 15:10

1 Answer 1

1

I've created an automator workflow that uses AppleScript and Bash to do what you want.

There are two steps to implementation:

  1. install translate-shell
  2. create an automator workflow (very easy)
  3. create a shortcut [optional]

You could probably use steps 1-2 to make a more specifically tailored applescript to copy the entire email and translate it, but I made a sample workflow that you could use just in case.

1: installation

translate-shell uses a number of different engines to translate using terminal. You don't have to understand most of this, just that you'll be using terminal to install it.

Go to the github page and install it: There are many ways to do this, but here is one

  • install homebrew: in terminal, type the following line: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)" and hit enter
  • install gawk: in terminal, type brew install gawk and hit enter
  • install trans-shell by typing in the following lines (see the website for troubleshooting):
    • git clone https://github.com/soimort/translate-shell
    • cd translate-shell/
    • make
    • make install (if this line doesn't work, try sudo make install and then type in your password.

Everything should now be installed in step 1

2: create an automator workflow – this is where the applescript is

  • Open automator (all macs have it pre-installed)
  • Select "Quick Action"
  • click the button that says "Workflow receives current 'Automatic (text)'" at the top and change it to 'text'
  • in the search bar "Name" search for "run applescript" and then double click the action that appears.
  • paste in the following script exactly as shown:

    on run {input, parameters} tell application "System Events" key code 8 using command down end tell set theText to the clipboard set theTranslation to (do shell script "export PATH=\"/usr/local/bin:$PATH\";/usr/local/bin/trans -b :en " & quoted form of theText) display dialog theTranslation end run

  • the applescript copies whatever text you have highlighted and uses a bash command to have translate-shell translate the text to English and display it.
  • save the workflow wherever and now you should be able to highlight any text in any app, right click > services > "your workflow name" to have a dialogue pop up with the translated text

3: Create a shortcut

If you'd rather highlight and use a shortcut, just create a shortcut to activate the workflow and translate any highlighted text. It's well documented in many places, but here's one stack exchange post where you can find the information.

Notes:

  • please let me know if you have any issues. Automator sometimes fights with apple's permissions, so you may have to grant it approval etc. I imagine, however, that if you know what applescripts are, you have run into this before.
  • I had a decent bit of trouble installing translate-shell and also tried using this command: brew install --HEAD translate-shell which is worth a shot
  • I often just use Keyboard Maestro for things like this. It works very well.
  • don't forget to mark the answer if this works :)
  • credit:
3
  • I am sorry but I think this does not answer my question. I do not need something to translate highlighted text (I already use Mate Translate for this). I need something to translate my emails and send them back to me already translated.
    – sintetico
    Jun 11, 2020 at 0:12
  • Ok I may try to fix this to suit your needs, but im pretty sure that the others who have been saying it would be really impractical are wrong. This script should be able to be modified so long as I can get the email processed into the text. Do you care about formatting? Jun 13, 2020 at 14:32
  • I don't care about the formatting of the email. Most of the emails that I get are only textual with simple formatting.
    – sintetico
    Jun 13, 2020 at 15:50

You must log in to answer this question.

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