How to make a script execute code? Topic is solved
- Gyro-Gearloose
- Posts: 121
- Joined: 03 Jan 2020, 16:13
How to make a script execute code?
Might be a silly question, but the way I got to know AHK was searching for keyboard shortcuts. That is define what a script should do when a certain keystroke was detected. But now I want a script to act as soon it is called, like closing other scripts (executables), where by the community was helping me a lot so far. Thx @vsub. Thx @boiler .
Re: How to make a script execute code?
Usually you just put the code at the top of the script (without a hotkey label in front), the so-called auto-execution section. Generally this sums it up best:
Of course you can also jump to labels via gosub or call functions (both are great to re-use code).
Of course, you can find loads of examples in the docs and all kind of forum posts. That's one of the best ways to learn anyway: Look at code of other people, try to understand, change it, play with it, look things up and if you can't find an answer, feel free to ask...https://www.autohotkey.com/docs/Scripts.htm#auto wrote:After the script has been loaded, it begins executing at the top line, continuing until a Return, Exit, hotkey/hotstring label, or the physical end of the script is encountered (whichever comes first). This top portion of the script is referred to as the auto-execute section. [...]
Code: Select all
#NoEnv
Sendmode Input
; etc - other general settings
msgbox this gets executed as soon you run this script
msgbox bye
ExitApp ; exits script
Re: How to make a script execute code? Topic is solved
If I understand what you’re saying, you typically specify a hotkey at the start of your scripts, requiring you to press that key to get the code to execute. And now you want a script to start executing without needing to press a hotkey.
If the above is correct, just don’t add a hotkey definition at the start of a script, and it will just execute its commands as soon as you run it. For example, the script below requires you to press F1 to see the MsgBox and whatever commands may follow:
This version of the script will show the MsgBox and execute whatever else follows it as soon as you run the script without waiting for a key press:
Is that what you mean?
Edit: What gregster said...
If the above is correct, just don’t add a hotkey definition at the start of a script, and it will just execute its commands as soon as you run it. For example, the script below requires you to press F1 to see the MsgBox and whatever commands may follow:
Code: Select all
F1::
MsgBox, This is the first command of my script
; more commands follow
Code: Select all
MsgBox, This is the first command of my script
; more commands follow
Edit: What gregster said...
Re: How to make a script execute code?
Set a hotkey to the script I posted and replace the ExitApp with Return and keep that script running all the time
I have a feeling that you are making simple tasks in an unnecessary difficult ways by compiling a separate script per task
I have a "master' script that does more than 30 different things depending on a situation and that script is running all that time so I can call a feature all the time without running multiple scripts
You don't need a separate script per hotkey or separate scripts using the same hotkey
I have a feeling that you are making simple tasks in an unnecessary difficult ways by compiling a separate script per task
I have a "master' script that does more than 30 different things depending on a situation and that script is running all that time so I can call a feature all the time without running multiple scripts
You don't need a separate script per hotkey or separate scripts using the same hotkey
- Gyro-Gearloose
- Posts: 121
- Joined: 03 Jan 2020, 16:13
Re: How to make a script execute code?
@boiler @gregster . Thank you so much. It was very good day when I discovered this forum. The youth of today is not that bad. I am assuming you are much younger than me.
Re: How to make a script execute code?
You're welcome. I'm not exactly a youth. My first computer was an Atari 800.
- Gyro-Gearloose
- Posts: 121
- Joined: 03 Jan 2020, 16:13
Re: How to make a script execute code?
@vsub. Thank you, too. I'll explain later (may be some weeks later) in the games subforum more precisely what I want to do and will see whether there is a more efficient, less complicated way.
Re: How to make a script execute code?
I am also not really a youth, although I look much younger than I am But you are welcome!Gyro-Gearloose wrote: ↑24 Jun 2020, 16:01@boiler @gregster . Thank you so much. It was very good day when I discovered this forum. The youth of today is not that bad. I am assuming you are much younger than me.
My first computer was a slightly used
Correction: I had a Sinclair ZX81, even worse. The one with the membrane keyboard . And 81 stands for 1981... but I got it used a bit later.
Who is online
Users browsing this forum: Chunjee, gsxr1300, inseption86, jaka1, mebelantikjaya and 289 guests