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
User avatar
Gio
Posts: 1247
Joined: 30 Sep 2013, 10:54
Location: Brazil

Re: Trouble getting FileAppend to work inside of a function

23 Nov 2017, 21:27

Hello BoogHoogLeson.

Welcome to the AutoHotkey community forums.

I don't see a function call in your code. Also, variables inside a function are local unless you forcibly declare them global. Try this:

Code: Select all

#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("Value of param1", "Value of param2") ; This is a function call.
FakeFunction("`nWriting this... ", "... In the file") ; This is another function call.


Return

FakeFunction(X, Y) ; This is a function definition. The code block below it leaves no doubt. Don't put it in the auto-execute section (see the return above).
{
	Global LogFile ; Declare this as a global variable so that function code can see the value you entered in the auto-execute section.
	LogLine := X . ", " . Y
	FileAppend, %LogLine%`n, %LogFile%
	
	return True
}
Best wishes.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Aqualest, downstairs, mikeyww, yanjujino1 and 274 guests