Why is reload not working here ?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Why is reload not working here ?

30 Oct 2023, 09:46

Hi guys, I have something strange happening what I cannot explain. Perhaps one of you could shed some light on the subject.
I seems like the reload in below snippet doesn't reset the script completely causing an error message if I run it a second time.

I was alway under the impression Reload would start from scratch just like you run the script for the first time.
Is that not the case ?

The funny thing is, if I do

Code: Select all

^r::reload
then my code works as intended. I STAND CORRECTED !! IT DOESN'T IT ALSO GIVES THAT ERROR MESSAGE

Part of my script has this code:

Code: Select all

if (Line = arr.Length)
        {
            A_Clipboard := ""
            Line := 1
            MsgBox("Before reload")       ; >>>>> THIS MSGBOX SHOWS UP ON MY SCREEN
            Reload
            Sleep(2000)
            MsgBox("After reload")		; >>>>> THIS ONE DOESN'T (AND SHOULDN'T) SHOW UP ON MY SCREEN
        }
The error message:

Error: Invalid index.

Specifically: 2

030: Reload()
031: }
▶ 032: Artikelnummer := arr[Line][9]
033: Omschrijving := arr[Line][10]
034: Aantal := RTrim(arr[Line][11], ",00")


Full script

Code: Select all

#Requires AutoHotkey v2.0
SetKeyDelay(100)
Line                 := 1
Omschrijving         := ""
Aantal               := ""
Land_van_oorsprong   := ""
Statistiekcode       := ""
Gewicht              := ""
Valuta               := ""

var := RTrim(A_Clipboard," `n`r")       ; Trim last empty line

Array.Prototype.DefineProp('Default', {Value: ""})				; could be Value: "n/a" or "n.v.t" in Dutch
arr := []
for x,y in strsplit(var,"`n","`r")
    {
    z := strsplit(y,a_tab)
    z.Length := 18						                        ; "sets" the array length of each line to 18
    arr.push(z)
    }               

^2::
{
    global Line+=1
    if !InStr(A_Clipboard, "Interne ID document")               ; Clipboard must contain the text "Interne ID document, otherwise reload the script"
        {
            MsgBox("Geen DHL Data gevonden !")
            Line := 1
            Reload
        }
    Artikelnummer       := arr[Line][9]
    Omschrijving        := arr[Line][10]
    Aantal              := RTrim(arr[Line][11], ",00")                        
    Land_van_oorsprong  := arr[Line][18]
    Statistiekcode      := StrReplace(StrReplace(arr[Line][17], "."), A_Space)      ; remove spaces and dots
    Gewicht             := StrReplace(arr[Line][15], ",",".")                       ; replace commas with dots
    Valuta              := arr[Line][14]
    ToolTip("Valuta = " . Valuta . " Aantal regels " . arr.Length)
    if (Valuta = "EUR")
        Bedrag              := StrReplace(arr[Line][12], ",",".")                   ; replace commas with dots
    else
        Bedrag              := StrReplace(arr[Line][13], ",",".")                   ; replace commas with dots
    if (Artikelnummer != "P")
        {
            Send(Omschrijving . "`t" . Aantal . "`t")   
            KeyWait "LButton", "D"
            Send(Land_van_oorsprong . "`r`t`t" . Statistiekcode . "`t")
            Send(Gewicht . "`t" . Bedrag) . "`r`t`t"
        }
    else
        {
            ToolTip("Klik op het porto veld.")
            KeyWait "LButton", "D"
            Send(Bedrag . "`r")
        }
    if (arr[2][14] != "EUR") AND (arr.Length = Line)
        {
            VV := (arr[2][14])
            ToolTip("Klik op het valuta veld. " . arr[2][14])
            Sleep(100)
            KeyWait "Lbutton", "D"
            Sleep(1000)
            Send(VV . "`r")
            ToolTip
        } 
    if (Line = arr.Length)
        {
            A_Clipboard := ""
            Line := 1
            MsgBox("Before reload")		; <<<<<<<<<<<THIS FIRES
            Reload
            Sleep(2000)
            MsgBox("After reload")			; <<<<<<<<<<<THIS DOESN'T
        }
}

^r::Reload

^e::ExitApp
User avatar
mikeyww
Posts: 27314
Joined: 09 Sep 2014, 18:38

Re: Why is reload not working here ?

30 Oct 2023, 12:37

Yes, Reload does what it says, which is to reload your script as if you first ran it. I recommend a Return statement following Reload.

To understand Reload more clearly, shorten your script, use ListLines, a MsgBox, etc.

The clipboard is a Windows resource, so if your script alters it, running the script again will not automatically restore the clipboard to its previous value.
User avatar
WarlordAkamu67
Posts: 231
Joined: 21 Mar 2023, 06:52

Re: Why is reload not working here ?

30 Oct 2023, 12:45

Maybe putting "#SingleInstance" would help, I doubt it.
As @mikeyww said, a return statement after the reload should help.

Reload:
reloadcontinued.PNG
reloadcontinued.PNG (45.91 KiB) Viewed 210 times

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: linkun, mcl, william_ahk and 32 guests