 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
rpnfan
Joined: 01 Aug 2007 Posts: 5
|
Posted: Fri Apr 25, 2008 8:38 pm Post subject: Suggestion for the tutorial in the help - script structure |
|
|
Hi,
when I started to use AHK about a year ago it took me quite some time to figure out some basic things. It become not clear to me how the general structure of a ahk script is. I propose the addition of the following notes in the help file. I think this is so helpful and important that I would add it as the fourth entry in the tutorial in the help file.
Here my suggestion for a new entry called "Structure of an AHK script"
| Quote: |
AutoHotkey scripts are structured as follows:
- AutoExecute section (runs until it encounters a Return)
- Subroutines and functions
Since your scripts first Return is the Return after the subroutine (btw: no braces needed), the subroutine will be executed.
; ----------------- Beginn Example --------------------
; ----------------- autoexecute section ---------------
MsgBox Hello from the autoexecute section
Gosub, MySubRoutine ; calls the subroutine
MyFunction() ; calls the function
Return ; End of AutoExecute section
; --------------- end autoexecute section -------------
MySubRoutine:
MsgBox Hello from the subroutine!
Return ; End of subroutine
MyFunction()
{
MsgBox Hello from the function!
}
; Function does not need a Return
; -------------- end example ------------------------ |
So the list of contents in the help file tutorial would list
Creating a script
Launching a program or document
Sending keystrokes and mouseclicks
Structure of an AHK script [NEW]
Activating and manipulating windows
.
.
.
What do you think? I hope my suggestion can be used (or modified) to update and improve the help file and give beginners a faster clue  |
|
| Back to top |
|
 |
Guest
|
Posted: Mon Apr 28, 2008 7:57 pm Post subject: |
|
|
| mmh, nobody thinks this is a good or bad idea? Or is this the wrong place for this posting? |
|
| Back to top |
|
 |
Guest
|
Posted: Tue Apr 29, 2008 3:08 am Post subject: |
|
|
| Maybe useful,maybe not.Not really needed urgly~ |
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 2359 Location: Australia, Qld
|
Posted: Tue Apr 29, 2008 6:58 am Post subject: |
|
|
I'm not sure why the distinction is necessary. The "auto-execute section" is really just a subroutine that runs when the script starts. There are only two places you can't put a function definition, iirc:
- Immediately following a label.
- Inside another function definition.
Clearly separating the auto-execute section from subroutine/function definitions (or not) is up to the individual. You could even explicitly label the auto-execute section, for clarity:
| Code: | AUTOEXECUTE:
MsgBox, This will be auto-executed because it is the first line of script.
return | It still needs to be at the top of the script, but it can also be explicitly called via gosub. |
|
| Back to top |
|
 |
Piz
Joined: 16 Feb 2008 Posts: 46
|
Posted: Tue Apr 29, 2008 4:59 pm Post subject: |
|
|
| Lexikos wrote: | | It still needs to be at the top of the script, but it can also be explicitly called via gosub. |
The autoexecute section doesn't have to be at the top of the script. Any code found anywhere outside function definitions will be executed (unless gosubs/gotos cause the code to be bypassed). For example, try this:
| Code: | first()
first()
{
msgbox %A_ThisFunc%
}
first()
second()
second()
{
msgbox %A_ThisFunc%
}
second() |
(I hope this runs - at the moment I'm at a computer where I can't run AHK, so I can't test it. It should illustrate the idea, though. Note that I'm not suggesting code be written like this. )
I have taken advantage of this from time to time, for example to create "initialization" code in included files that executes merely because it's in the file and not within any functions (though I don't do it that way any more and don't recommend it - see below).
Out of habit from lots of programming over the years, I always put my autoexecute sections after my function definitions. I also use global variables to define "constants," and I place those before the functions (so that the functions can use any "constants" I've defined).
Note that, because AHK executes any code it finds outside of functions,* when designing modules to be included, authors should not assume they will be included at any specific place in a script, and write them so they will not interfere with code execution that may occur before and/or after them. (They should also not expect that any of their code that is outside their functions will be executed, because the including file may "detour" around them.)
_____
*This is not the case for function libraries - only functions actually called from them are executed. |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|