Trouble getting FileAppend to work inside of a function

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
BoogHoogleson
Posts: 4
Joined: 23 Nov 2017, 17:13

Trouble getting FileAppend to work inside of a function

23 Nov 2017, 17:55

I've spent a decent amount of time experimenting and Googling and this is driving me nuts so I decided to make an account here and ask for help. Hello!

When I use FileAppend via a hotkey to test it out, it works just fine (see PgDn). However, when I try to do the same thing inside of a function, it doesn't write to the file nor does it throw any kind of error (see PgUp). What's happening here and what can I do to fix this?

Code: Select all

;
; AutoHotkey Version: 1.x
; Language:       English
; Platform:       Win9x/NT
; Author:         A.N.Other <[email protected]>
;
; Script Function:
;	Template script (you can customize this template by editing "ShellNew\Template.ahk" in your Windows folder)
;

#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.

LogFile = %A_ScriptDir%\Log.txt
FileDelete %LogFile%

FakeFunction(X, Y)
{
	LogLine := X . ", " . Y
	FileAppend, %LogLine%`n, %LogFile%
	
	return True
}

PgUp::
	if (FakeFunction(200,500))
	{
		MsgBox True
	} else {
		MsgBox False
	}
return

PgDn::
	X := 200
	Y := 500
	
	LogLine := X . ", " . Y
	FileAppend, %LogLine%`n, %LogFile%
return

Del:: 
	ExitApp 
return
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Trouble getting FileAppend to work inside of a function

23 Nov 2017, 18:04

You need to either add "global LogFile" to the top of your script or add "global" to the top of your function.

Code: Select all

global LogFile
LogFile = %A_ScriptDir%\Log.txt
OR

Code: Select all

FakeFunction(X, Y)
{
     global
Not doing either means that the variable used inside of your function is local to that function (i.e. a different variable than the one that you defined above).
User avatar
BoogHoogleson
Posts: 4
Joined: 23 Nov 2017, 17:13

Re: Trouble getting FileAppend to work inside of a function

23 Nov 2017, 18:18

When I try to do the first suggestion I am greeted with this error when I open the script: "This line does not contain a recognized action."

However, the latter works. I'll do some more reading into global variables. Thanks a lot!
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Trouble getting FileAppend to work inside of a function

23 Nov 2017, 18:54

The former works for me in the latest (1.1.26.01) version of AutoHotkey. Perhaps you're using a pre-v1.1.05 version. From the help file:
Super-global variables [v1.1.05+]: If a global declaration appears outside of any function, it takes effect for all functions by default. This avoids the need to redeclare the variable in each function. However, if a function parameter or local variable with the same name is declared, it takes precedence over the global variable. Variables created by the class keyword are also super-global.
Edit: Also, make sure that you separate the declaration and the assignment, as I did. "global LogFile = %A_ScriptDir%\Log.txt" isn't permitted, apparently.
User avatar
BoogHoogleson
Posts: 4
Joined: 23 Nov 2017, 17:13

Re: Trouble getting FileAppend to work inside of a function

23 Nov 2017, 19:25

Yep, it's been a long time since I installed/updated AHK. I'm using v1.0.48.05.

Thanks again for all the help!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Araphen, mebelantikjaya and 322 guests