There are several ways to create a function while coding your script, some will code it into their current working project and run the whole project multiple times when testing the new function, I know it, because for 4 years I DID IT. Or you could Code it in its own file and save it, then run the main script to test it.
Then one day on the boards i saw a function that was different, it had :
if A_Scriptname=formatseconds.ahk
{
exitapp
}
At the start of it, simply to close it, if it was run directly!
I now fill it with all sorts of code to test the function fully and when done just block it out using /* and */
It is one snippit of code i now use regularly when creating a function or regularly run code i call from my main projects.
I have saved a lot of time using it and think there is a lot of people in this Community that might actually benefit from it so i have attached a function i use a lot, (As an example), for finding out how long a MP3 file is after stripping "Time" from the Tags. But using it directly to test it. (I Commented the Hell out of it because my ADHD was pinging today )
if A_Scriptname=formatseconds.ahk ;<< THIS FUNCTIONS FILE NAME.
{
; All code between the brackets is ignored when calling the actual function from a main script!
#SingleInstance Force ; Helps when testing multiple times in a function like Formatseconds
result:=formatseconds(160.001)
msgbox,,Function Testing %A_Scriptname%, Converting 160.001 seconds`nYour Result is : %result%
; You can CODE pretty much anything, even #Include a script here and it works without affecting the function Below, building or testing a function like this can be a lot quicker and leave your main code intact while you work directly inside your function library.
; You can also call other functions in here that are in you Library store..
; when finished just */ /* your test code before you save and close it, and only the function will be compiled with your script, ignoring your test code.
exitapp
}
FormatSeconds(NumberOfSeconds,delim="1") ; Converts the specified number of seconds to hh:mm:ss format.
{
time = 19990101 ; *Midnight* of an arbitrary date.
time += %NumberOfSeconds%, seconds
FormatTime, mmss, %time%, mm:ss
if delim
{
stringreplace,mmss,mmss,:,m ;change : to m
mmss=%mmss%s ;add s to end
}
return mmss
}
I havent coded a function in my main code now for years....
You can simplify that if statement to if (A_LineFile == A_ScriptFileFullPath). This checks if the current file is the same string as the full, initial file ran. Basically AHK's version of if __name == '__main__':
Totally understood, similar amount of typing, same result, I do try to offer a noob an easier way to read how it works, some code makes sense immediately, albeit, the help file can decode a more intense line that is not immediately understood by a noob. I also see that line can be used in the default new ahk file and never need changing, just rem it to disable. Nice alternative.