How to make a script execute code? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Gyro-Gearloose
Posts: 121
Joined: 03 Jan 2020, 16:13

How to make a script execute code?

24 Jun 2020, 15:27

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 .
gregster
Posts: 9012
Joined: 30 Sep 2013, 06:48

Re: How to make a script execute code?

24 Jun 2020, 15:36

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:
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. [...]
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...

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
Of course you can also jump to labels via gosub or call functions (both are great to re-use code).
User avatar
boiler
Posts: 16951
Joined: 21 Dec 2014, 02:44

Re: How to make a script execute code?  Topic is solved

24 Jun 2020, 15:43

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:

Code: Select all

F1::
MsgBox, This is the first command of my script
; more commands 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:

Code: Select all

MsgBox, This is the first command of my script
; more commands follow
Is that what you mean?

Edit: What gregster said... :D
vsub
Posts: 541
Joined: 29 Sep 2015, 03:39

Re: How to make a script execute code?

24 Jun 2020, 15:51

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
User avatar
Gyro-Gearloose
Posts: 121
Joined: 03 Jan 2020, 16:13

Re: How to make a script execute code?

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.
User avatar
boiler
Posts: 16951
Joined: 21 Dec 2014, 02:44

Re: How to make a script execute code?

24 Jun 2020, 16:11

You're welcome. I'm not exactly a youth. My first computer was an Atari 800. :D
User avatar
Gyro-Gearloose
Posts: 121
Joined: 03 Jan 2020, 16:13

Re: How to make a script execute code?

24 Jun 2020, 16:12

@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.
gregster
Posts: 9012
Joined: 30 Sep 2013, 06:48

Re: How to make a script execute code?

24 Jun 2020, 16:33

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.
I am also not really a youth, although I look much younger than I am :) But you are welcome!
My first computer was a slightly used Sinclair ZX Spectrum - who would use something like this, if there had been many options ?!? :D

Correction: I had a Sinclair ZX81, even worse. The one with the membrane keyboard :problem:. And 81 stands for 1981... but I got it used a bit later.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, gsxr1300, inseption86, jaka1, mebelantikjaya and 289 guests