Currently, DeepL (https://www.deepl.com/translate) is hot in the translators world.
Could anyone please create a macro to translate the content of the current clipboard and replace it with a translation in one of the available languages?
It would also be very useful to have any available alternative translations displayed (possibly via another keyboard shortcut).
Via the computer magazine C’t (https://www.heise.de/ct/hotline/DeepL-Uebersetzer-auf-der-Kommandozeile-3825206.html?wt_mc=print.ct.2017.20.162#zsdb-article-links) I’ve found this Python script:
But I don’t have the knowledge to get it working on my Mac :(.
Thanks in advance!
Hi @CafeTran_Training, I'm not an expert in scripts, so I created this macro. Maybe it'll help you a little.
You have to select the languages with the cursor, because you cannot use Enter etc. here. Everything else is done by the macro.
11)Übersetzen.kmmacros (34,6 KB)
Greetings from Germany
Assuming you have never fiddled around with python on your Mac, I recommend you this:
Install the Homebrew package manager according to the instructions.
Now open a new Terminal window and install python3 with the help of Homebrew:
brew install python3
Wait for the install to finish.
Install the required python modules
Since you have installed python3 in a separate environment, do not follow the instructions on the Github page.
Instead, open a new Terminal window and just run this:
python3 -m pip install requests
This was the only module I had to install.
Save the script from the Github page
anywhere on your disk as
Run the script from within KM
in a Execute Shell Script action:
/usr/local/bin/python3 '<path/to/the/script>/translate.py' -l DE "$(pbpaste)"
Of course, replace
<path/to/the/script> with the actual path to your script file.
Here is an example macro:
[demo] DeepL via Python.kmmacros (1.7 KB)
This will feed the text on your clipboard to the script and you'll get the output in a KM window.
-l option in the command line is for the target language. (Obviously using the ISO 639-1 codes in uppercase.)
Since you have linked to this thread on two other forums, I assume that my above instructions worked fine for you.
On the CafeTran forum you say that “you need the uber-useful Keyboard Maestro to execute the macros.” Just for clarification:
The Python script itself runs perfectly fine also without KM. You can run it with other launchers (Alfred, LaunchBar, etc.) or from within an AppleScript, etc., or of course simply from the Terminal.
In the Terminal, to get the result to the clipboard just pipe it to
pbcopy. If your system’s
LANG variable is correctly set to UTF-8 (the language doesn’t matter) then you don’t need the first line.
/usr/local/bin/python3 '<path/to/the/script>/translate.py' -l DE "$(pbpaste)" | pbcopy
would be a standalone command line for the Terminal that gets the input from the clipboard and pastes the output to the clipboard.
BTW, I’ve never heard of that CafeTran thingy before, but it looks very interesting. I’m also working as translator (not my main job though) and in the past I used OmegaT or, ahem, no CAT tool at all. I will definitely give CafeTran a serious try next time!
I’ve just seen that the forum links you’ve posted on Proz and Freshdesk go to post#2 in this thread. I’m not sure if this was your intention, since you also uploaded the ‘DeepL via Python’ macro file (which is from post #3).
Yes, Tom, your instructions helped me to get the Python script running. Thanks again!
The piping to the clipboard is also very nice. Must look into Alfred some day …
About CafeTran: I’ve compared it with the other currently available CAT (computer-assisted translation) tools for the Mac platform thoroughly. It’s an advanced, high-performance app, with a very responsive developer. If you have any questions about CafeTran, I’d be very happy to help you!
Hi @CafeTran_Training ,
4 days ago Translate Shell has added support for DeepL.
I have uploaded a GUI macro for Translate Shell in the Macros category.
Unfortunately, with DeepL via Translate Shell there are encoding issues on the Mac. I’ll try to find a workaround, when I find some time. Or better: let’s hope the author finds some solution…
Found sort of a workaround. See the macro post.
Is there a way to get these nifty alternative translations from DeepL in Safari or Chrome to a variable?
I don’t have time to do that at present, but here’s a quick and dirty way.
Run the following AppleScript from an Execute an AppleScript action:
tell application "Safari" to tell front document to return its text
This will return all the text from the front page in Safari.
I guess it will be easier to use the Translate Shell script, which supports DeepL. Read the docs on Github, it has stuff like dictionary mode and alternate translations (not sure if also for DeepL, but I think so).
If Translate Shell does what you want, then it’s just a matter of grabbing the shell output, which will probably be more straightforward than getting it from the web page. (The script is using the API of the transllation engine.)
I’ve already written a KM macro for Translate Shell. It does not cover the alternate translations and other modes of Translate Shell, but I’m sure you can copy some elements from the macro.
You also find some (hopefully) useful information in my macro post, e.g. how to install Translate Shell and a workaround for a bug (if the bug is still there).
According to this Wiki article you can use KM variables in the JS action.
So, setting a variable to the Clipboard token
%SystemClipboard% and then using that variable as shown in the Wiki article should work.
Hint: If you posted a downloadable minimal example (+ the information what you are trying to achieve on exactly which web page URL) we could test it for you.
I’ve tried your “Send Clipboard Content to DeepL” macro in Safari (with the proper Safari actions) and it doesn’t work. But it doesn’t work either if I replace the KM variable with a fix string (
const strClip = "test string";).
Does it work for you in Chrome?
Well, I installed Chrome, and it works in Chrome. And it also works perfectly fine with the KM variable (
const strClip = document.kmvar.clipText;)
So, ATM I don’t know why it doesn’t work for you with the Clipboard token / KM variable. My Chrome version is 65.0.3325.181
Which KM version are you using?
If you are using KM7 or earlier try it with
%CurrentClipboard% (instead of
It is possible that
%SystemClipboard% was introduced with KM8.
I’m using the latest versions of Chrome and Keyboard Maestro.
I just downloaded your macro again, to make sure I haven’t made any modification. And yes, it works out of the box here.
(To be clear: I’m speaking of your “Send Clipboard” macro, not any of the others.)