Why A_Index cause unknown name error Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
DanielToward13
Posts: 71
Joined: 18 May 2017, 10:56

Why A_Index cause unknown name error

01 Dec 2017, 13:07

I need to retrieve some tr tags from a webpage so I use MyTablee := MyTable.getElementsByTagName("tr")[A_Index] but A_Index cause unknown name error. The code works when I replace A_Index with numbers like MyTablee := MyTable.getElementsByTagName("tr")[2]. I faced this problem before but I don't what is the reason and how to fix it?

Code: Select all

#Persistent
#SingleInstance Force
#NoEnv  ;~ Recommended for performance and compatibility with future AutoHotkey releases.

SendMode Input  ;~ Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ;~ Ensures a consistent starting directory.
SetTitleMatchMode, 3

ToolTipVisible = 0
;~ Interface for selecting source and target language
Gui, 1:Add, Text, x10 y15 vTb1, Source language:
Gui, 1:Add, Combobox, x+20 yp-3 vLangIn, auto||en|fr|fr|it|es|nl|ja|sk|tr|hu ;~ place double pipe behind language to be used as default
Gui, 1:Add, Text, x10 y+15 vTb2, Target language:
Gui, 1:Add, Combobox, x+22 yp-3 vLangOut, en|fr||fr|it|es|nl|ja|sk|tr|hu ;~ place double pipe behind language to be used as default
Gui, 1:Add, Checkbox, x10 y+20 Checked vPaste2CB, Paste translation to clipoard when closing tooltip
Gui, 1:Add, Checkbox, x10 y+15 Checked vHideToolTip, Hide tooltip on mouseclick
Gui, 1:Add, Button, x30 y+20 w80 vB1, &Exit
Gui, 1:Add, Button, x+30 w80 Default vB2, &OK
Gui, 1:Show, Autosize
Return

ButtonExit:
GuiEscape:
GuiClose:
ExitApp

ButtonOK:
Gui, 1:Submit
Return

;~ [Ctrl]+[F12] shows the little user interface for changing source and target language
^F12::
Gui, 1:Show, Autosize
Return

;~ A mouse click makes the tooltip disappear
~RButton::
~MButton::
~LButton::
If HideToolTip = 0
    Return
If ToolTipVisible = 1
{
    ToolTip
    ToolTipVisible = 0
    Return
}
Return

;~ [F12] runs Google Translate for selected text or makes the tooltip with the translation disappear if it is visible
F12::
If ToolTipVisible = 1
{
    ToolTip
    ToolTipVisible = 0
    Return
}
CurrentCB = %Clipboard%
Clipboard =
SendInput, ^c
ClipWait, 5
If ErrorLevel
{
    MsgBox, 48, GoogleTranslateSelection, No text highlighted or problem copying text to clipboard.
    Return
}
Source = %Clipboard%
StringLen, SourceLength, Source
SourceLength := SourceLength * 5
ToolTip, Translating... please wait ?., % A_CaretX-SourceLength, % A_CaretY+50
Target =
Target := GoogleTranslate(Source,LangIn,LangOut)
ToolTip, %Target%, % A_CaretX-SourceLength, % A_CaretY+50
ToolTipVisible = 1
If Paste2CB = 1
    Clipboard = %Target%
Else
    Clipboard = %CurrentCB%
Return

GoogleTranslate(Source,LangIn,LangOut)
{
    StringReplace, Source, Source, %A_Space%, `%20, All
    Base := "translate.google.com/#"
    Path := Base . LangIn . "/" . LangOut . "/" . Source
    WMP := ComObjCreate("InternetExplorer.Application") ;~ Creation of hidden Internet Explorer instance to look up Google Translate and retrieve translation
    WMP.Navigate(Path)
    While WMP.readyState!=4 || WMP.document.readyState!="complete" || WMP.busy
            Sleep 50
    
    MyTable := WMP.document.getElementsByClassName("gt-cd-c")[5].getElementsByTagName("table")[0]
    Loop, % MyTable.rows.length {
		if (A_Index > 1) {
			MyTablee := MyTable.getElementsByTagName("tr")[A_Index]
			Result .= MyTablee.getElementsByTagName("td")[1].innerText
		}
    }
    WMP.Quit
    Return Result
}




MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: Why A_Index cause unknown name error

01 Dec 2017, 17:00

Try using %A_Index% instead of just A_Index. MyTable.getElementsByTagName("tr")[%A_Index%]
DanielToward13
Posts: 71
Joined: 18 May 2017, 10:56

Re: Why A_Index cause unknown name error

01 Dec 2017, 17:49

It still gives me the same error.
User avatar
Xtra
Posts: 1829
Joined: 02 Oct 2015, 12:15

Re: Why A_Index cause unknown name error  Topic is solved

01 Dec 2017, 17:54

0 based array should use:

Code: Select all

MyTablee := MyTable.getElementsByTagName("tr")[A_Index - 1]
HTH
A_AhkUser
Posts: 1118
Joined: 06 Mar 2017, 16:18
GitHub: AAhkUser
Location: France

Re: Why A_Index cause unknown name error

01 Dec 2017, 17:56

Hi,

[...]


[EDIT]
Looks like Xtra preceded me (btw I'd like to see the photo-finish... :lol: ).
Instead of delete this simple repetition I'll indicate here some references concerning your first error (the use of the percent sign) noted by MaxAstro. It is described here in the documentation. It describes what's happen when a variable is enclosed in percent signs within an expression (actually, an expression is strore in a varuable whenever one use the colon-equal operator).
Last edited by A_AhkUser on 01 Dec 2017, 18:17, edited 2 times in total.
my scripts
DanielToward13
Posts: 71
Joined: 18 May 2017, 10:56

Re: Why A_Index cause unknown name error

01 Dec 2017, 18:13

Thanks. It solves the problem. Some tr tag's value are null or sometimes some elements does not exist so the code returns unknown name error again. How can I check if the return value is null then skip it?
User avatar
Xtra
Posts: 1829
Joined: 02 Oct 2015, 12:15

Re: Why A_Index cause unknown name error

01 Dec 2017, 19:16

Use: Try

Code: Select all

Try
    MyTablee := MyTable.getElementsByTagName("tr")[A_Index - 1]

Return to “Ask For Help”

Who is online

Users browsing this forum: Botsy, DanRim, DMDComposer, k0rey777, mabohango, patilizer, PlumpSamurai, sebbulon, twentw, w0z and 49 guests