Extending the File Object?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
stretch65
Posts: 21
Joined: 16 Jun 2015, 23:49

Extending the File Object?

Post by stretch65 » 11 Dec 2020, 07:10

I don't have much experience with OOP in AHKv2, but I was wondering if it's
possible to add an extra function to the File object in a script.

The function I had in mind would do the same thing as File.WriteLine( "some text" ),
but would also send "some text" to my debugger (using "OutputDebug").

Is that even possible?

User avatar
kczx3
Posts: 1649
Joined: 06 Oct 2015, 21:39

Re: Extending the File Object?

Post by kczx3 » 11 Dec 2020, 09:49

I don't believe that the File class is currently available globally from script. There's other classes that a similar strategy has to be used with I think. You basically have to use FileOpen to create an instance of a File object and then define a method on that object's base property. Something like this. You could obviously open any file first to create the method. I just chose StdOut.

Code: Select all

#Persistent

; Open a console window for this demonstration:
DllCall("AllocConsole")

f := FileOpen("*", "W")
f.base.DefineMethod("WriteLineDebug", (this, string) => !OutputDebug(string) && this.WriteLine(string))
f.close()

f := FileOpen("*", "W")
f.WriteLineDebug("This is a test")
f.close()

stretch65
Posts: 21
Joined: 16 Jun 2015, 23:49

Re: Extending the File Object?

Post by stretch65 » 12 Dec 2020, 00:22

Thanks very much for this.

Post Reply

Return to “Ask for Help (v2)”