How to make an AppleScript to automatically translate emails from a foreign language and redirect them to yourself?
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
- Translate the email (with google translate for example)
- 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.
I've created an automator workflow that uses AppleScript and Bash to do what you want.
There are two steps to implementation:
- install
translate-shell
- create an automator workflow (very easy)
- 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, trysudo 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:
- https://github.com/soimort/translate-shell/wiki/AppleScript
- https://forum.keyboardmaestro.com/t/gui-for-translate-shell/8868