leuce Posted November 8, 2016 Share Posted November 8, 2016 (edited) Hello everyone I have a script that works in AU3 form, but not in drag/drop EXE form. It tells me "Line 6816 Error: Variable used without being declared". I've tested the script with Au3Check and there were no errors and no warnings. I tried Au3Stripper to include all the Includes, so that I can see the line at which the error occurs, but the resultant file has only 247 lines, nowhere near 6816. I used MsgBox instances in various places to find out at which point the error occurs, and I think I found it (see [#1] in the attached script). I found that if I add "Global $inputfile" to the top of the file, AutoIt no longer complained about line 6816, but... it then complains about line 6873. Again using MsgBox instances I tried to narrowed down the offending variable, but I'm not sure if I've found it ($searchstring), (see [#2]). Further fiddling yielded no luck. The script works fine as both AU3 and EXE if I just double-click, but not with drag/drop. The purpose of the script is to send text to the Google Translate API. You can run it but without a Google Translate API key you may not be able to test it beyond the second error. Got any idea what might be the issue? Thanks Samuel expandcollapse popup#include <Inet.au3> #include <Array.au3> 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) ; [#1] MsgBox (0, "", "y", 0) ; to see where the error occurs... it occurs here. ; Adding Global $inputfile to the top of the script fixes this $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 () ; [#2] MsgBox (0, "searchstring", $searchstring, 0) ; which turns up as "&q=" in the drag/drop version, but which is supposed to contain (among other things) 20 lines from the input file! ; Adding Global $j, $numberofrepeats, $inputfilesplit, $startrowindex, $endrowindex to the top of the script does not fix this, unfortunately. 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") $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 simplegoog v2#12345#en#af.au3 Edited November 8, 2016 by leuce Link to comment Share on other sites More sharing options...
Developers Jos Posted November 8, 2016 Developers Share Posted November 8, 2016 11 minutes ago, leuce said: but the resultant file has only 247 lines Did you add this line to just do the MergeOnly as I get 6885 lines after running au3stripper: #Au3Stripper_Parameters=/MO Likely $InputFile is the issue as it is defines within an If-EndIf and not by a Global statement. Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
leuce Posted November 9, 2016 Author Share Posted November 9, 2016 Thanks, Jos, I did not realise that I had to add the "MO" line to the top of the script itself. The stripper now results in a more believable 7000+ line file. Link to comment Share on other sites More sharing options...
leuce Posted November 13, 2016 Author Share Posted November 13, 2016 (edited) I eventually found the problem. The user was supposed to be able to specify the input file in three ways: by drag and drop, by FileOpenDialog, and by ClipGet. Now, way, way at the start of the script I had neglected to FileRead the $CmdLine[1] variable. This meant that $inputfile was declared if the user was using the clipboard or had manually selected the input file, but not when the user was using drag-and-drop. The final script (i.e. the version that does what it should, and nothing more) is attached. simplegoog (for forum, without exe).zip Edited November 13, 2016 by leuce Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now