DeepL autotranlation in AutoHotKey

Post your working scripts, libraries and tools for AHK v1.1 and older
mora145
Posts: 57
Joined: 25 Jun 2022, 15:31

DeepL autotranlation in AutoHotKey

Post by mora145 » 29 Jan 2023, 04:55

Hello friends. Here I leave a small video about a script that I use to quickly translate text from English to any language you set in DeepL.

Requeriments:
Descolada Lib: https://github.com/Descolada/UIAutomation
You will need create a folder in AutoHotKey system folder, named Lib, and you need place the files of the lib here.
Example:
C:\Program Files\AutoHotKey\Lib\[Files lib] (UIA_Interface.ahk,UIA_Constants.ahk,UIA_Browser.ahk)

"CNTRL + <" is the actually hotkey to run the translation

This has a shortcut on the keyboard to use it and what it does is that it selects all the text from the place or text box where we have the cursor, and sends it to DeepL to be translated (DeepL must be set to 'Translate text withing the DeepL app Control CC' as the script sends this key combination to translate the text. After that it copies the result to the clipboard, so all you have to do is paste the text.
In this case it is an English version, so the texts of DeepL must be in English (You can modify it to add any other).
This script is not perfect even though I use it a lot, when translating text quickly.



Special thanks to @Descolada for his automation library, this script makes use of it.

Code: Select all

#Include <UIA_Interface>

        global UIA := UIA_Interface() ; Initialize UIA interface

        VerifyDeepL:
        if not WinExist("ahk_exe DeepL.exe"){
            
            RunWait %comspec% /c ""C:\Users\%A_UserName%\AppData\Roaming\Programs\Zero Install\0install-win.exe" run --no-wait https://appdownload.deepl.com/windows/0install/deepl.xml",,Hide
            TrayTip, Timed TrayTip, DeepL is not running. Starting.
            SetTimer, HideTrayTip, -3000
            HideTrayTip() {
                TrayTip
            }
            Sleep, 5000
            Goto, VerifyDeepL
        }

        VerifyDeepL1:
        global cEl := UIA.ElementFromHandle("DeepL")
        if (cEl=) {
            Sleep, 2000
            Goto, VerifyDeepL1
        }

        MsgBox,0,,Script is running,2

;Translate the Text in the clipboard
;LanguagueTarget = the language you want to be translated. In this case it will be English. This prevents DeepL from changing the source language.
TranslationFunc(LanguagueTarget,copySource:=true){
    MouseGetPos ,OutputVarX, OutputVarY
        ;if Clipboard is empy, return
        if (Clipboard =) {
            return
        }
        ;if copySource = true, send LControl+G
        if (copySource = true){
            Send {LControl down}{g}
            Sleep, 500
            Send {LControl up}
        }
    Send {LControl down}{a}
    Sleep, 100
    Send {LControl up}
    WinGet, active_id, ProcessName, A
    Sleep, 500
    Send {LControl down}{c} ;press Left Control and C key two times
    Sleep, 100
    Send {c}
    Send {LControl up} ;up Left Control 
    Sleep, 2000 ;wait 2000ms
    WinMove, DeepL,, 0, 0, 100, 200 ;move DeepL to the corner and rezise it to 100x200
    Sleep, 5
    WinGetActiveTitle, active_id2 
        if (Button1 := cEl.FindFirstBy("Name=' (detected)' AND ControlType=Text")){
            ;search for "Name= (detectado) and ControlType=Text"
            Languague := Button1.FindByPath("-1", UIA.CreateCondition("ControlType", "Text"))
            Languague_name := Languague.GetCurrentPropertyValueEx("Name") ;extrac the "Name" in the element
            StringLower Languague_name, Languague_name ;lowercase Languague_name  
            
                if (NOT Languague_name = LanguagueTarget) {
                Sleep, 1000
                ;WinMaximize, %active_id2%
                Sleep, 500
                cEl.FindFirstByName("Swap source with target language").Click()
                Sleep, 1000
                cEl.FindFirstBy("NAME=" LanguagueTarget " AND ControlType=Button",,,false).Click()
                WinMove, DeepL,, 0, 0, 100, 100
                Sleep, 2000
                }

        }

    cEl.WaitElementExistByName("Copy",,,,15000)
    sleep, 500
    cEl.FindFirstBy("Name=Copy").Click()
    sleep, 200
    WinActivate, ahk_exe %active_id%
    Sleep, 500
    Send {LControl up}

return
}

^<::
TranslationFunc("English",false)
return
Last edited by mora145 on 31 Jan 2023, 11:08, edited 1 time in total.

Return to “Scripts and Functions (v1)”