garry wrote:an die idee mit einem gui (viewtopic.php?f=9&t=67721#p291156)
teadrinker wrote:für dein übersetzer (zeile: 301) und mp3 (zeile: 422) skript
tmplinshi wrote:für dein AutoXYWH (zeile: 466) skript und die idee wie man die größe speichern kann (privat posteingang)
rommmcek wrote:für das modifizieren das die eingabesprache als wert zurückgegeben wird (zeile: 305, 329)
malcev wrote: für das hinzufügen des Spam Schutz beim übersetzer Skript (zeile: 333)
@UnReALiTyy
- Übersetzen von markiertem oder kopiertem Text mit Taste "F3"
- fügte eine GUI hinzu mit einem eingabe und ausgabe feld
- eine text datei mit drag and drop öffnen
- speichern und laden der button, gui größe und position
- zeigt text wenn die maus über dem button ist
- übersetze seiten beim kopieren des links
- ändern der Gui größe durch ziehen
Buttons:
- ein zum verschieben des textes
- ein zum übersetzen des eingetragenen textes
- zwei zum kopieren des textes fürs rechte und linke feld
- ein zum leeren der felder
- ein um Alwaysontop an und auszuschalten
- einen um den text im browser zu öffnen
- einen um eine text datei zu öffnen
- einen um den übersetzten text zu speichern
- einen um die orginal gui größe wiederherzustellen
- einen um die neue gui größe zu speichern
- einen um die gespeicherte gui größe zu laden
- einen um die übersetzung als mp3 zu speichern
- ein um die übersetzung abzuspielen
- zwei dropdownlisten die die erkannte/ausgewählte sprache anzeigen
-------------------------------------------------------------------------------------
Update: Code Optimierungen v.(03.11.23)
Wer Bugs findet oder Ideen zum hinzufügen hat, ich bin für alles offen und Dankbar.
-------------------------------------------------------------------------------------
Code: Select all
/*===================================================================================
thanks to:
@garry the idea with a gui (https://www.autohotkey.com/boards/viewtopic.php?f=9&t=67721#p291156)
@teadrinker for your translator (line: 301) and mp3 (line: 422) script
@tmplinshi for your AutoXYWH (line: 466) script and the idea how to save the size (private inbox)
@rommmcek for modifying that the input language is returned as a value (line: 305, 329)
@malcev for adding spam protection to the translator script (line: 333)
-------------------------------------------------------------------------------------
@UnReALiTyy added the complete gui
===================================================================================
*/
#SingleInstance, force
#KeyHistory 0
#NoEnv
SetBatchLines, -1
SetWinDelay, -1
ListLines, Off
SendMode, Input
SetTitleMatchMode, 2
Critical
global tl1
goto, language
;=========================================================================================================
guiWeb:
link := StrReplace(clipboard, "%"), link := StrReplace(link, ":", "%3A"), link := StrReplace(link, "/", "%2F"), link := StrReplace(link, "?", "%3F"), link := StrReplace(link, "=", "%3D"), link := StrReplace(link, "#", "%23"), link := StrReplace(link, "&", "%26"), link := StrReplace(link, " ", "%20"), link := StrReplace(link, "`n", "%0A"), open .= StrReplace(link, """"), clipboard := safe
Run, % open
GuiClose:
GuiEscape:
Settimer, savedXY, off
ToolTip
2GuiClose:
2GuiEscape:
Gui, Destroy
Gui, 2:Destroy
return
savedXY:
WinGetPos, savedX, savedY,,, ahk_class AutoHotkeyGUI
return
GuiDropFiles:
Loop, Parse, A_GuiEvent, `n
file := A_LoopField
goto, readfile
;=========================================================================================================
F3::
Critical
safe := clipboard, btn6 := 0
Send ^{c}
Sleep, 50
if (SubStr(clipboard,1,4) = "http") or (SubStr(clipboard,1,4) = "www.") {
open = https://translate.google.com/translate?sl=auto&tl=%tl1%&u=
goto, guiWeb
}
Gui, 1:new, +AlwaysOnTop +Resize +Minsize630x275
Gui, Add, StatusBar, vsb, `r ↑Text Copy or Write, Open or Drop a File.↑%A_Tab%%A_Tab%v.(03.11.23) by UnReALiTyy
Gui, Add, DDL, w100 x%dd1X% y27 vlist1 gDDL1, %e5x%
Gui, Add, DDL, w100 x%dd2X% y27 vlist2 gDDL2, %e5x%
Gui, Add, Button, x%b1X% y2 vbtn1 gb1, <Move>
Gui, Add, Button, x%b2X% y26 vbtn2 gb2 Default, Translate
Gui, Add, Button, x%b3X% y26 vbtn3 gb3, ↓Copy
Gui, Add, Button, x%b4X% y26 vbtn4 gb4, Copy↓
Gui, Add, Button, x%b5X% y26 vbtn5 gb5, ↓Clear↓
Gui, Add, Button, x%b6X% y2 vbtn6 gb6 Default, Top
Gui, Add, Button, x%b7X% y2 vbtn7 gb7, ↓Web
Gui, Add, Button, x%b8X% y2 vbtn8 gb8, Restore
Gui, Add, Button, x%b9X% y2 vbtn9 gb9, Open
Gui, Add, Button, x%b10X% y2 vbtn10 gb10, Save
Gui, Add, Button, x%b11X% y2 vbtn11 gb11, New
Gui, Add, Button, x%b12X% y2 vbtn12 gb12, Load
Gui, Add, Button, x%b13X% y2 vbtn13 gb13, MP3
Gui, Add, Button, x%b14X% y2 vbtn14 gb14, Play
Gui, Add, Edit, vedt1 w%edtW% h%edtH% x10 y50 +Limit65535 +WantTab, % clipboard
Gui, Add, Edit, vedt2 w%edtW% h%edtH% x%edtX% y50 +WantTab
GuiControl, Focus, Translate
GuiControl, Choose, list1, % tl1
gosub, b2
Gui, 1:Show, w%savedW% h%savedH% x%savedX% y%savedY%, Translate to %tl1%
OnMessage(0x200, "WM_MOUSEMOVE")
Settimer, savedXY, 200
clipboard := safe
return
;=========================================================================================================
b3: ;Copy Left
b4: ;Copy Right
Gui, Submit, nohide
(A_Thislabel = "b3" ? clipboard := edt1 : clipboard := edt2)
return
b1: ;Move
Gui, Submit, nohide
GuiControl, Text, edt1, % edt2
GuiControl, Choose, list1, % list2
DDL1: ;DropDownList1
Gui, Submit, nohide
tl1 := SubStr(list1,1,2)
WinSetTitle, ahk_class AutoHotkeyGUI,, Translate to %tl1%
IniWrite, %tl1%, %rssini%, Lang, key
;return ;disables translating when selecting a language from the drodownlist or Move Text
b2: ;Translate
DDL2: ;DropDownList2
Gui, Submit, nohide
gosub, Gui2
if (A_Thislabel = "DDL2")
GuiControl, 1:Text, edt2, % (trl := GoogleTranslate(edt1, SubStr(list2,1,2))).1
else {
GuiControl, 1:Text, edt2, % (trl := GoogleTranslate(edt1)).1
GuiControl, 1:Choose, list2, % trl.2
}
Gui, 2:Destroy
if (trl.1)
return
MsgBox, 262192, attempt Failed!, Errors when translating the text, try again or change the text!`n`nAn Internet connection is needed!
exit
b5: ;Clear
GuiControl, Text, edt2
GuiControl, Text, edt1
return
b6: ;Top
Winset, Alwaysontop, Toggle, ahk_class AutoHotkeyGUI
GuiControl, % ((btn6 := !btn6) ? "+Default" : "-Default"), Top
return
b7: ;Web
Gui, Submit, nohide
clipboard := edt1
open = https://translate.google.com/?sl=auto&tl=%tl1%&text=
goto, guiWeb
b8: ;Restore
Gui, 1:Show, w630 h275 x635 y375, Translate to %tl1%
return
b9: ;Open
Gui +OwnDialogs
FileSelectFile, file, 3,, Select file, Text documents (*.txt)
if (!file)
return
readfile:
FileRead, text, %file%
GuiControl,, edt1, %text%
return
b10: ;Save
Gui +OwnDialogs
FileSelectFile, file, S16,, Save file, Text documents (*.txt)
if (!file)
return
if !InStr(file, ".txt")
file .= .txt
if FileExist(file)
FileDelete %file%
Gui, Submit, nohide
FileAppend, %edt2%, %file%
return
b11: ;New
IniWrite, %savedW%, %rssini%, Gui, W
IniWrite, %savedH%, %rssini%, Gui, H
IniWrite, %savedX%, %rssini%, Gui, X
IniWrite, %savedY%, %rssini%, Gui, Y
ControlGetPos, edtX,, edtW, edtH, Edit2
IniWrite, %edtW%, %rssini%, Edit, W
IniWrite, %edtH%, %rssini%, Edit, H
IniWrite, % edtX - 8, %rssini%, Edit, X
Loop, 14 {
ControlGetPos, b%A_Index%X,,,, Button%A_Index%
IniWrite, % b%A_Index%X - 8, %rssini%, Buttons, xb%A_Index%
}
Loop, 2 {
ControlGetPos, list%A_Index%X,,,, ComboBox%A_Index%
IniWrite, % list%A_Index%X - 8, %rssini%, DropDownList, xDDL%A_Index%
}
return
b12: ;Load
Settimer, savedXY, off
IniRead, savedW, %rssini%, Gui, W
IniRead, savedH, %rssini%, Gui, H
IniRead, savedX, %rssini%, Gui, X
IniRead, savedY, %rssini%, Gui, Y
Gui, 1:Show, w%savedW% h%savedH% x%savedX% y%savedY%, Translate to %tl1%
Settimer, savedXY, 200
return
b13: ;MP3
Gui +OwnDialogs
FileSelectFile, file, S16,, Save file, Audio (*.mp3)
if (!file)
return
if !InStr(file, ".mp3")
file .= .mp3
if FileExist(file)
FileDelete, %file%
Gui, Submit, nohide
GetAudioFromGoogle(SubStr(edt2,1,200), tl1, file)
if !FileExist(file)
MsgBox, 262192, Attempt Failed!, Error creating the audio!
return
b14: ;Play
file = %a_scriptdir%\lib\tempPlay.mp3
Gui, Submit, nohide
cut := 1
Loop {
GetAudioFromGoogle(SubStr(edt2,cut,200), tl1, file)
if (ErrorLevel = 1)
break
SoundPlay, %file%, Wait
FileDelete, %file%
cut += 200
}
return
;=========================================================================================================
language:
e5x = af_Afrikaans|sq_Albania|am_Amharic|ar_Arabic|hy_Armenian|az_Azerbaijani|eu_Basque|bn_Bengalese|my_Burmese|bs_Bosnian|bg_Bulgarian|ceb_Cebuano|ny_Chichewa|zh-CN_Chinese|de_German|da_Danish|en_English|eo_Esperanto|et_Estonia|tl_Filipino|fi_Finnish|fr_French|fy_Friesian|gl_Galician|ka_Georgian|el_Greek|gu_Gujarati|ht_Haitian|ha_Hausa|haw_Hawaiian|iw_Hebrew|hi_Hindi|hmn_Hmong|ig_Igbo|id_Indonesian|ga_Irish|is_Icelandic|it_Italian|ja_Japanese|jw_Javanese|yi_Yiddish|kn_Kannada|kk_Kazakh|ca_Catalan|km_Khmer|rw_kinyarwanda|ky_Kyrgyz|ko_Korean|co_Corsican|hr_Croatian|ku_Kurdish|lo_Lao|la_Latin|lv_Latvian|lt_Lithuanian|lb_Luxembourgish|mg_Malagasy|ml_Malayalam|ms_Malaysian|mt_Maltese|mi_Maori|mr_Marathi|mk_Macedonian|mn_Mongolian|ne_Nepalese|nl_Dutch|no_Norwegian|or_Odia|ps_Pashto|fa_Persian|pl_Polish|pt_Portuguese|pa_Punjabi|ro_Romanian|ru_Russian|sm_Samoan|gd_Scottish|sv_Schedisch|sr_Serbian|st_Sesotho|sn_Shona|sd_Sindhi|si_Singhalese|sk_Slovak|sl_Slovenia|so_Somalia|es_Spanish|sw_Swahili|su_Sundanese|tg_Tajik|ta_Tamil|tt_Tatar|te_Telugu|th_Thai|cs_Czech|tr_Turkish|tk_Turkmen|ug_Uighur|uk_Ukrainian|hu_Hungarian|ur_Urdu|uz_Uzbek|vi_Vietnamese|cy_Welsh|be_Belarusian|xh_Xhosa|yo_Yoruba|zu_Zulu
btn1_TT := "Move right field to the left", btn2_TT := "Translate left field", btn3_TT := "Copy left field", btn4_TT := "Copy right field", btn5_TT := "Clear fields", btn6_TT := "Gui lock/unlock", btn7_TT := "View in browser", btn8_TT := "Restore size", btn9_TT := "Open a text file", btn10_TT := "Save the translation", btn11_TT := "Save new size", btn12_TT := "Load saved size", btn13_TT := "Save as mp3", btn14_TT := "Play the Text", list1_TT := "Select a language", list2_TT := "Detected language"
rssini = %a_scriptdir%\Translator.ini
if !FileExist(rssini) {
FileAppend, [Lang]`nkey=de`n===========`n[Gui]`nW=630`nH=275`nX=635`nY=375`n===========`n[Edit]`nW=300`nH=200`nX=320`n===========`n[Buttons]`nxb1=288`nxb2=490`nxb3=10`nxb4=578`nxb5=290`nxb6=589`nxb7=10`nxb8=60`nxb9=200`nxb10=240`nxb11=110`nxb12=145`nxb13=553`nxb14=340`n===========`n[DropDownList]`nxDDL1=380`nxDDL2=110, %rssini%
Gui, 3:New, -SysMenu +ToolWindow +AlwaysOnTop
Gui, Add, Progress, vProgress w120
Gui, Add, Text, vText wp
Gui, Show,, Installation is in progress!
Loop, 100 {
GuiControl,, Progress, %A_Index%
GuiControl,, Text, % "Install..." A_Index "%"
Sleep 1
}
Gui, 3:Destroy
MsgBox, 262192, file created!, The file "Translator.ini" was created in ["%a_scriptdir%"]`n`nMark or copy text you want to translate and press the F3 key.
}
IniRead, tl1, %rssini%, Lang, key
IniRead, savedW, %rssini%, Gui, W
IniRead, savedH, %rssini%, Gui, H
IniRead, savedX, %rssini%, Gui, X
IniRead, savedY, %rssini%, Gui, Y
IniRead, edtW, %rssini%, Edit, W
IniRead, edtH, %rssini%, Edit, H
IniRead, edtX, %rssini%, Edit, X
Loop, 14
IniRead, b%A_Index%X, %rssini%, Buttons, xb%A_Index%
Loop, 2
IniRead, dd%A_Index%X, %rssini%, DropDownList, xDDL%A_Index%
return
;=========================================================================================================
GuiSize:
if (A_EventInfo = 1) {
Settimer, savedXY, off
WinWaitActive, ahk_class AutoHotkeyGUI
Settimer, savedXY, 200
return
}
AutoXYWH("w0.5h", "edt1")
AutoXYWH("x0.5w0.5h", "edt2")
AutoXYWH("x0.5", "btn1", "btn5", "btn14")
AutoXYWH("x0.75", "btn2")
AutoXYWH("x", "btn4", "btn6", "btn13")
AutoXYWH("x0", "btn3", "btn7")
AutoXYWH("x0.35", "btn9", "btn10")
AutoXYWH("x0.15", "btn8", "btn11", "btn12")
AutoXYWH("x0.75", "list1")
AutoXYWH("x0.25", "list2")
savedW := A_GuiWidth
savedH := A_GuiHeight
Settimer, Redraw, -50
Redraw:
WinSet, Redraw,, ahk_class AutoHotkeyGUI
return
WM_MOUSEMOVE() {
static CurrControl, PrevControl, _TT
CurrControl := A_GuiControl
if (CurrControl != PrevControl)
ToolTip % %CurrControl%_TT
PrevControl := CurrControl
}
Gui2:
if (StrLen(edt1) > 5000) {
MsgBox, 262192, Error !, The copied text is too long!`nReduce the number of characters.`nAnd then try again.`n(max. 5.000!)
exit
}
Gui, 2:New, +Toolwindow +AlwaysOnTop
Gui, Add, Text,, Translation is in progress!
Gui, Show, AutoSize, Please wait
return
;=========================================================================================================
;https://www.autohotkey.com/boards/viewtopic.php?f=6&t=63835
GoogleTranslate(str, from := "auto", to := "0") {
static JS := CreateScriptObj(), _ := JS.( GetJScript() ) := JS.("delete ActiveXObject; delete GetObject;")
json := SendRequest(JS, str, to, from, proxy := "")
oJSON := JS.("(" . json . ")"), lng := oJSON[2] ;modified by @rommmcek
if !IsObject(oJSON[1]) {
Loop % oJSON[0].length
trans .= oJSON[0][A_Index - 1][0]
}
else {
MainTransText := oJSON[0][0][0]
Loop % oJSON[1].length {
trans .= "`n+"
obj := oJSON[1][A_Index-1][1]
Loop % obj.length {
txt := obj[A_Index - 1]
trans .= (MainTransText = txt ? "" : "`n" txt)
}
}
}
if !IsObject(oJSON[1])
MainTransText := trans := Trim(trans, ",+`n ")
else
trans := MainTransText . "`n+`n" . Trim(trans, ",+`n ")
from := oJSON[2]
trans := Trim(trans, ",+`n ")
return [trans, lng] ;modified by @rommmcek
}
;https://www.autohotkey.com/boards/viewtopic.php?f=6&t=63835&p=320080#p319790
SendRequest(JS, str, tl, sl, proxy) {
static http
ComObjError(false)
loop
{
if !http
{
http := ComObjCreate("WinHttp.WinHttpRequest.5.1")
( proxy && http.SetProxy(2, proxy) )
http.open( "get", "https://translate.google.com", 1 )
http.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0")
http.send()
http.WaitForResponse(-1)
}
http.open( "POST", "https://translate.google.com/translate_a/single?client=webapp&sl="
. sl . "&tl=" . tl1 . "&hl=" . tl1
. "&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&otf=0&ssel=0&tsel=0&pc=1&kc=1"
. "&tk=" . JS.("tk").(str), 1 )
http.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
http.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0")
http.send("q=" . URIEncode(str))
http.WaitForResponse(-1)
if !InStr(http.responsetext, "<form id=""captcha-form""")
break
http := ""
}
return http.responsetext
}
URIEncode(str, encoding := "UTF-8") {
VarSetCapacity(var, StrPut(str, encoding))
StrPut(str, &var, encoding)
While code := NumGet(Var, A_Index - 1, "UChar") {
bool := (code > 0x7F || code < 0x30 || code = 0x3D)
UrlStr .= bool ? "%" . Format("{:02X}", code) : Chr(code)
}
return UrlStr
}
GetJScript()
{
script =
(
var TKK = ((function() {
var a = 561666268;
var b = 1526272306;
return 406398 + '.' + (a + b);
})());
function b(a, b) {
for (var d = 0; d < b.length - 2; d += 3) {
var c = b.charAt(d + 2),
c = "a" <= c ? c.charCodeAt(0) - 87 : Number(c),
c = "+" == b.charAt(d + 1) ? a >>> c : a << c;
a = "+" == b.charAt(d) ? a + c & 4294967295 : a ^ c
}
return a
}
function tk(a) {
for (var e = TKK.split("."), h = Number(e[0]) || 0, g = [], d = 0, f = 0; f < a.length; f++) {
var c = a.charCodeAt(f);
128 > c ? g[d++] = c : (2048 > c ? g[d++] = c >> 6 | 192 : (55296 == (c & 64512) && f + 1 < a.length && 56320 == (a.charCodeAt(f + 1) & 64512) ?
(c = 65536 + ((c & 1023) << 10) + (a.charCodeAt(++f) & 1023), g[d++] = c >> 18 | 240,
g[d++] = c >> 12 & 63 | 128) : g[d++] = c >> 12 | 224, g[d++] = c >> 6 & 63 | 128), g[d++] = c & 63 | 128)
}
a = h;
for (d = 0; d < g.length; d++) a += g[d], a = b(a, "+-a^+6");
a = b(a, "+-3^+b+-f");
a ^= Number(e[1]) || 0;
0 > a && (a = (a & 2147483647) + 2147483648);
a `%= 1E6;
return a.toString() + "." + (a ^ h)
}
)
return script
}
CreateScriptObj() {
static doc
doc := ComObjCreate("htmlfile")
doc.write("<meta http-equiv='X-UA-Compatible' content='IE=9'>")
return ObjBindMethod(doc.parentWindow, "eval")
}
;=========================================================================================================
;https://www.autohotkey.com/boards/viewtopic.php?f=6&t=63835&start=40#p316531
GetAudioFromGoogle(text, lng, mp3filePath) {
url := CreateUrl(text, lng)
data := SendRequest2(url)
if SubStr(data, 1, 6) = "Error!"
return data
return SaveDataToFile(data, mp3filePath)
}
CreateUrl(text, lng) {
JS := CreateScriptObj2(), JS.( GetJScript() )
url := "https://translate.google.ru/translate_tts?ie=UTF-8&tl="
. lng . "&total=1&idx=0&client=t&prev=input&textlen="
. StrLen(text) . "&tk=" . JS.("tk").(text) . "&q=" . URIEncode(text)
return url
}
SendRequest2(url) {
whr := ComObjCreate("Msxml2.XMLHTTP.6.0")
whr.Open("GET", url, false)
whr.Send()
if (whr.Status != 200)
return "Error! Status: " . whr.Status . "`n`n" . whr.responseBody
return whr.responseBody
}
SaveDataToFile(data, filePath) {
stream := ComObjCreate("ADODB.Stream")
stream.type := 1 ; Binary data
stream.Open
stream.Write(data)
stream.SaveToFile(filePath, 2)
stream.Close
return true
}
CreateScriptObj2() {
static doc
doc := ComObjCreate("htmlfile")
doc.write("<meta http-equiv='X-UA-Compatible' content='IE=9'>")
Return ObjBindMethod(doc.parentWindow, "eval")
}
;=========================================================================================================
;http://ahkscript.org/boards/viewtopic.php?t=1079
AutoXYWH(DimSize, cList*){
static cInfo := {}
If (DimSize = "reset")
Return cInfo := {}
For i, ctrl in cList {
ctrlID := A_Gui ":" ctrl
If ( cInfo[ctrlID].x = "" ){
GuiControlGet, i, %A_Gui%:Pos, %ctrl%
GuiControlGet, Hwnd, %A_Gui%:Hwnd, %ctrl%
MMD := InStr(DimSize, "*") ? "MoveDraw" : "Move"
fx := fy := fw := fh := 0
For i, dim in (a := StrSplit(RegExReplace(DimSize, "i)[^xywh]")))
If !RegExMatch(DimSize, "i)" dim "\s*\K[\d.-]+", f%dim%)
f%dim% := 1
cInfo[ctrlID] := { x:ix, fx:fx, y:iy, fy:fy, w:iw, fw:fw, h:ih, fh:fh, gw:A_GuiWidth, gh:A_GuiHeight, a:a , m:MMD}
}Else If ( cInfo[ctrlID].a.1) {
dgx := dgw := A_GuiWidth - cInfo[ctrlID].gw , dgy := dgh := A_GuiHeight - cInfo[ctrlID].gh
For i, dim in cInfo[ctrlID]["a"]
Options .= dim (dg%dim% * cInfo[ctrlID]["f" dim] + cInfo[ctrlID][dim]) A_Space
GuiControl, % A_Gui ":" cInfo[ctrlID].m , % ctrl, % Options
} } }