#include #include Global $searchstring Global $bigtranslation = "" #cs Rename your script from simplegoog###.au3 to simplegoog#YOUR_API_KEY#source_language#target_language.au3 1. Script gets API key, SL and TL from its own name. 2. Script asks for input file (or gets it from EXE or clipboard). 3. Ask user if it's okay to split by sentence. If yes, use abbreviation list find/replace. 4. Split input file by CRLF. 5. Use 20 lines at a time, encode them, add them to the URL, and send it to GT. 6. Parse replies: To parse JSON reply: Split by: "translatedText": " Then split [2] by " ; Also encode/decode any URL text. ; http://www.w3schools.com/tags/ref_urlencode.asp And write to a file. With a TrayTip telling the progress. #ce ; Determine if script file name contains "googx" or "googxy" If StringInStr (@ScriptName, "googx") Then ; no prompts, split by paragraph $noprompt = 1 $cliporno = 6 $scriptnamesplitinfo = 6 Else $noprompt = 0 EndIf If StringInStr (@ScriptName, "googxy") Then ; no prompts, split by sentence $splitbysentence = 6 Else $splitbysentence = 7 EndIf ; Get API key, SL and TL from script file name $scriptnamesplit = StringSplit (@ScriptName, "#", 1) If $scriptnamesplit[0] = 4 Then $apikey = $scriptnamesplit[2] $SL = $scriptnamesplit[3] $TL = StringTrimRight ($scriptnamesplit[4], 4) If $noprompt = 0 Then $scriptnamesplitinfo = MsgBox (4, "Is this information correct?", "API key: " & $apikey & @CRLF & "Source language code: " & $SL & @CRLF & "Target language code: " & $TL, 0) EndIf If $scriptnamesplitinfo = 7 Then MsgBox (0, "Error 1", "Could not get API key, source language code or target language code from script file name." & @CRLF & "The script will now exit.", 0) Exit EndIf Else MsgBox (0, "Error 2", "Could not get API key, source language code or target language code from script file name." & @CRLF & "The script will now exit.", 0) Exit EndIf ; Get input file (either via drag/drop, clipboard or file select) If $CmdLine[0] > 0 Then $inputfileopendialog = $CmdLine[1] Else If $noprompt = 0 Then $cliporno = MsgBox (4, "Translate the clipboard?", "Do you want to translate the clipboard, or a file?" & @CRLF & "Say 'Yes' to translate the clipboard.", 0) EndIf If $cliporno = 6 Then $inputfile = ClipGet () If @error > 0 Then If $noprompt = 0 Then MsgBox (0, "Clipboard error", "Unable to access clipboad, or clipboard is empty.", 0) EndIf Exit EndIf Else $inputfileopendialog = FileOpenDialog ("Source text file", @ScriptDir, "Text files (*.txt)|All files (*.*)") $inputfile = FileRead (FileOpen ($inputfileopendialog, 128)) $outputfile = FileOpen ($inputfileopendialog & "_output.txt", 129) EndIf EndIf ; Load abbreviation list, for sentence segmentation If FileExists (@ScriptDir & "\abbreviationlist.txt") Then $abbreviationlistfile = FileRead (FileOpen (@ScriptDir & "\abbreviationlist.txt", 128)) $abbreviationlist = StringSplit ($abbreviationlistfile, ",", 1) Else $abbreviationlist = StringSplit ("Apr.,Aug.,Corp.,D.,Dec.,Dept.,Dr.,D.C.,e.g.,etc.,Feb.,Jan.,Jun.,Jul.,Jr.,Inc.,i.e.,Lt.,Ltd.,Mar.,max.,min.,Mr.,Mrs.,Ms.,Oct.,Nov.,Pr.,Pres.,Rev.,rev.,Sep.,Sgt.,St.,Str.,vol.,vs.", ",", 1) EndIf ; Optionally, split input file further by sentence. If $noprompt = 0 Then $splitbysentence = MsgBox (4, "Segment further by sentence?", "Try to segment further by sentence?" & @CRLF & "Say 'No' if your text has one sentence per line already.", 0) EndIf If $splitbysentence = 6 Then $inputfile = StringReplace ($inputfile, ".", ".###") $inputfile = StringReplace ($inputfile, "!", "!###") $inputfile = StringReplace ($inputfile, "?", "?###") For $i = 1 to $abbreviationlist[0] $inputfile = StringReplace ($inputfile, $abbreviationlist[$i] & "###", $abbreviationlist[$i], 0, 1) Next $inputfile = StringReplace ($inputfile, "###", @CRLF) $inputfile = StringReplace ($inputfile, @CRLF & " ", @CRLF) EndIf ; Split input file by line MsgBox (0, "", "x", 0) ; to see where the error occurs $inputfilesplit = StringSplit ($inputfile, @CRLF, 1) MsgBox (0, "", "y", 0) ; to see where the error occurs... it occurs here. $startrowindex = 1 $endrowindex = 20 $numofstrings = 0 $numberofrepeats = Int ($inputfilesplit[0] / 20) ; + 1 $finalitems = $inputfilesplit[0] - ($numberofrepeats * 20) ; First do all the multiples of 20 For $j = 1 to $numberofrepeats $searchstring = "&q=" & _ArrayToString ($inputfilesplit, "&q=", $startrowindex, $endrowindex) $startrowindex = $startrowindex + 20 $endrowindex = $endrowindex + 20 DoIt () Next ; Then do the last few, after all the multiples of 20 $endrowindex = $startrowindex + $finalitems - 1 $searchstring = "&q=" & _ArrayToString ($inputfilesplit, "&q=", $startrowindex, $endrowindex) DoIt () ; Then give user end-report If $cliporno = 6 Then ClipPut ($bigtranslation) If $noprompt = 0 Then MsgBox (0, "Translation on clipboard", "The translation is on the clipboard.", 0) Else $mousey = MouseGetPos () ToolTip ("The translation is on the clipboard.", $mousey[0], $mousey[1], "", 1, 1) Sleep ("2000") EndIf EndIf ; =================================== Func DoIt () If StringRight ($searchstring, 3) = "&q=" Then $searchstring = StringTrimRight ($searchstring, 3) EndIf $searchstring = StringReplace ($searchstring, "&q=&q=", "&q=") $searchstring = StringReplace ($searchstring, "&q=&q=", "&q=") $searchstring = StringReplace ($searchstring, "&q=&q=", "&q=") $searchstring = StringReplace ($searchstring, "%", "%25") ; $searchstring = StringReplace ($searchstring, "&q=-", "&q=%2D") ; $searchstring = StringReplace ($searchstring, "$", "%24") ; $searchstring = StringReplace ($searchstring, "/", "%2F") ; $searchstring = StringReplace ($searchstring, "@", "%40") ; $searchstring = StringReplace ($searchstring, ":", "%3A") MsgBox (0, "searchstring", $searchstring, 0) ; which turns up empty in the drag/drop version! $foo = InetRead ("https://www.googleapis.com/language/translate/v2?key=" & $apikey & "&source=" & $SL & "&target=" & $TL & $searchstring, 0) If @error Then ClipPut ("https://www.googleapis.com/language/translate/v2?key=" & $apikey & "&source=" & $SL & "&target=" & $TL & $searchstring) MsgBox (0, "Error #" & @error, "Google is unhappy with the request. We've added the URL to the clipboard. Try pasting it in a browser and see if there's an error message." & @CRLF & @CRLF & "Also double-check if the URL you paste is the same as the URL that is left after the page is visited. If it says ""400, bad request"" then the search string may contain characters that we need to escape... or perhaps you use an invalid API key.", 0) EndIf $foo = BinaryToString ($foo, 4) If StringInStr ($foo, '"errors": [') Then If $noprompt = 0 Then MsgBox (0, "Google throws an error...", $foo, 0) EndIf Else $foosplit = StringSplit ($foo, '"translatedText": "', 1) For $m = 2 to $foosplit[0] $foosplit2 = StringSplit ($foosplit[$m], '"', 1) $translation = $foosplit2[1] $translation = StringReplace ($translation, "\u200b", "") $translation = StringReplace ($translation, "'", "'") $translation = StringReplace ($translation, """, '"') If $cliporno = 6 Then $bigtranslation = $bigtranslation & $translation & @CRLF Else FileWrite ($outputfile, $translation & @CRLF) EndIf If $noprompt = 0 Then $numofstrings = $numofstrings + 1 TrayTip ("Progress...", $numofstrings & " done so far out of " & $inputfilesplit[0], 100) EndIf Next EndIf EndFunc