1
votes

I want to create a script that lets me send strings from an array, one by one at the press of a hotkey. (Press once and the first line is sent, press again and the second line is sent and so on) but my (so far limited) comprehension of AutoHotKey fails me.

This is what I have so far (”borrowed” the bit on constructiong the array from the ahk - site)

;Write to the array:
ArrayCount = 0
Loop, Read, C:\My_little_dir\test.txt{ ;test.txt contains 6-digit numbers separated only by ENTER/newline.
    ArrayCount += 1  ; Keep track of how many items are in the array.
    Arr_Bookings%ArrayCount% := A_LoopReadLine  ; Store this line in the next array element.
}

element=1

Change(direction, element, ArrayCount){
    if (direction = "next"){
        ;incrementing from the last element gets us back to the first element
        if (element = %ArrayCount%)
            {element=1}
        else
            {element+=1}
    }
    else{
        if (direction = "previous"){
            ;decrementing from the first element gets us back to the last element
            if (element=0)
            {element=%ArrayCount%}
        else
            {element-=1}
        }
    }
Return Arr_Bookings%element%
}

#N::Send % Change(next,element, ArrayCount)
#B::Send % Change(previous,element, ArrayCount)

However, when I run it, I get an errormessage:

Line Text: #N::Send Change(next,element, ArrayCount)

Error:Hotkeys/hotstrings are not allowed inside functions.

I’ve checked over and over again for messed up curly braces, but to no avail (whitespace carrys no significance...right?).

Any ideas what's causing this?

Also, if you see anything else horribly wrong with this code, feel free to mention it.

Thanks in advance! /Leo

1

1 Answers

0
votes

Autohotkey does not like your indentation style. Use the Allman style.

i.e. put every bracket in its own line and don't use it if you don't have to; for example:

if (element = %ArrayCount%)
    {element=1}
else
    {element+=1}

braces here are completely superfluous.

I normally wouldn't do this but since i already have the code, here is your unrolled code:

;Write to the array:
ArrayCount = 0
Loop, Read, C:\My_little_dir\test.txt
{ ;test.txt contains 6-digit numbers separated only by ENTER/newline.
    ArrayCount += 1  ; Keep track of how many items are in the array.
    Arr_Bookings%ArrayCount% := A_LoopReadLine  ; Store this line in the next array element.
}

element=1

Change(direction, element, ArrayCount)
{
    if (direction = "next")
    {
        ;incrementing from the last element gets us back to the first element
        if (element = %ArrayCount%)
            {
            element=1
            }
        else
            {
            element+=1
            }
    }
    else
    {
        if (direction = "previous")
        {
            ;decrementing from the first element gets us back to the last element
            if (element=0)
            {
            element=%ArrayCount%
            }
        else
            {
            element-=1
            }
        }
    }
Return Arr_Bookings%element%
}

#N::Send Change(next,element, ArrayCount)
#B::Send Change(previous,element, ArrayCount)