Question

I'm making a script to load emoticons from a file and print it, but I'm struggling to get it to work because everything I read from it is blank. I'm new so I'm sure I'm doing something wrong, could anyone have a look at my code and tell me what it is? (The absolute address is right)

The three strings are for the tooltip to show what's above and below, size is the number of emoticons, Line is the current emoticon, pg up and dn cycle, and end types the emoticon. So far, I'm puzzled as to why none of the file read lines set the strings as empty. Nothing, not even the End block, work.

#InstallKeybdHook
SendMode Event
#UseHook

File:= "C:\Users\Ben\Desktop\ahk\emoticons.txt"
Line := 0
size := 0
FileReadLine, string, %File%, %Line%
FileReadLine, string1, %File%, mod(%Line%-1, size)
FileReadLine, string2, %File%, mod(%Line%+1, size)

Loop, read, %File%
{
    size += 1
}

start:

ToolTip, %string%`n%string1%`n%string2%, 100, 100, 1

~PgUp::
{
    Line:=mod(%Line%+1, size)
    FileReadLine, string, %File%, %Line%
    FileReadLine, string1, %File%, mod(%Line%-1, size)
    FileReadLine, string2, %File%, mod(%Line%+1, size)
}
return

~PgDn::
{
    Line:=mod(%Line%-1, size)
    FileReadLine, string, %File%, %Line%
    FileReadLine, string1, %File%, mod(%Line%-1, size)
    FileReadLine, string2, %File%, mod(%Line%+1, size)
}
return

~End::
{
    FileReadLine, str, %File%, %Line%
    Send %str%
}
return

goto, start
Was it helpful?

Solution

Nothing too complicated, there are just a few changes to make.

  1. For FileReadLine, the line count starts a 1 and not 0 unlike other languages. So trying to read line 0, FileReadLine will essentially "read" nothing.

  2. For the mod() function, the documentation says :

    If the second parameter is zero, the function yields a blank result (empty string).

    Logically, when the script starts, and size is 0, FileReadLine will try to read line number "blank". As I recall, "blank" is not a number.

  3. mod(%Line%-1, size) is an expression. That means, that everything is parsed as an expression here. Mod() will return its calculated value like normal, if successful (consider #2). if we consider %Line%-1, parsed as expression, here is what we'll have :
    Let's say we have var:=4 and var_b:=(%var%-1). One might think that var_b would be equal to 3, but that is false. In var_b:=(%var%-1), the contents of var will be evaluated as the name of a variable and then the contents of that variable. This means that it will take 4 and evaluate it as the variable named 4 and NOT the value 4. Unless the variable 4 has been set, it will return "blank". As I recall (again), "blank" is not a number. In this case, I imagine the intended expression was mod(Line-1, size).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top