Script only works in isolation

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Joey5
Posts: 131
Joined: 19 May 2023, 05:25

Script only works in isolation

05 May 2024, 08:08

I need help with the following script. I have a .ahk file that contains different kinds of scripts. All scripts in it work fine. Combining all in to one script obviously makes it easier to load all. The following script works fine when executed separately. However, when I add it to the larger file and it fails to work. Other scripts continue to work - just not this script. Does it have to be modified? Help greatly appreciated.

Code: Select all

; Define the commands for each tap
commands := ["{numpad0}", "{numpad1}", "{numpad2}", "{numpad3}", "{numpad8}"]

; Initialize the counter
counter := 1

; Hotkey for the "\" key
f4::
    ; Execute the corresponding command
    Send, % commands[counter]

    ; Increment the counter
    counter := (counter = 5) ? 1 : counter + 1
return
User avatar
mikeyww
Posts: 28872
Joined: 09 Sep 2014, 18:38

Re: Script only works in isolation

05 May 2024, 08:15

Hello,

Your top statements probably do not execute. I would read the following section.

:arrow: Auto-execute section
After the script has been loaded, it begins executing at the top line, continuing until a Return, Exit, the script's first hotkey/hotstring label, or the physical end of the script is encountered (whichever comes first).
Source: Scripts - Definition & Usage | AutoHotkey v1
Joey5
Posts: 131
Joined: 19 May 2023, 05:25

Re: Script only works in isolation

05 May 2024, 08:20

I read the section. Unfortunately, it is too rich for me. I could not figure out how to modify it so it works with other scripts. Any further help will be appreciated.
User avatar
mikeyww
Posts: 28872
Joined: 09 Sep 2014, 18:38

Re: Script only works in isolation

05 May 2024, 08:35

Move your variable definitions to the top of the script. If they remain below a Return command, they will never execute, because the Return executes first.

If you have a script that does not work, you can post it in this forum to get feedback about it.
Joey5
Posts: 131
Joined: 19 May 2023, 05:25

Re: Script only works in isolation

05 May 2024, 08:48

I am not sure I understand. In the following script, there is nothing below return. Thank you.

Code: Select all

; Define the commands for each tap
commands := ["{numpad0}", "{numpad1}", "{numpad2}", "{numpad3}", "{numpad8}"]

; Initialize the counter
counter := 1

; Hotkey for the "\" key
f4::
    ; Execute the corresponding command
    Send, % commands[counter]

    ; Increment the counter
    counter := (counter = 5) ? 1 : counter + 1
return
User avatar
mikeyww
Posts: 28872
Joined: 09 Sep 2014, 18:38

Re: Script only works in isolation

05 May 2024, 08:49

Instead of posting the script that works, I recommend posting the script that does not work.
Joey5
Posts: 131
Joined: 19 May 2023, 05:25

Re: Script only works in isolation

05 May 2024, 09:04

The script works as is. But not when added to series of other scripts. For example, in the following example, it has been added at the end of other scripts. In this scenario, it does not work. TY.

Code: Select all

; Long press the escape key to reload and reset all scripts
~$*Esc::
KeyWait, Esc, T1
IF !ErrorLevel
	Return ; if held for less than 1 second
SoundPlay, %A_WinDir%\Media\chord.wav, 1
For all, LF in [[0x1FF,"sc{:X}"],[0xFE,"vk{:X}"]]
    Loop,% LF.1 ;release all pressed keys
        IF (""<Key:=GetKeyName(Format(LF.2, A_Index)))
            SendInput, {%Key% up}{Esc}
Reload



;toggle holding down the right and left mouse button for wheelless scrolling
f3::
alt := not alt
if (alt)
{
Click Down
Click, Down Right
}
else
{
Click Up
Click, Up Right
}
Return


; Define the commands for each tap
commands := ["{numpad0}", "{numpad1}", "{numpad2}", "{numpad3}", "{numpad8}"]

; Initialize the counter
counter := 1

; Hotkey for the "\" key
f4::
    ; Execute the corresponding command
    Send, % commands[counter]

    ; Increment the counter
    counter := (counter = 5) ? 1 : counter + 1
return
User avatar
mikeyww
Posts: 28872
Joined: 09 Sep 2014, 18:38

Re: Script only works in isolation

05 May 2024, 09:14

The script works by starting at the top. Commands are executed until a Return or Exit command is reached. The first hotkey in a script has an implicit Return just before it. To understand what your script does, follow it line by line, from the top. In your new post, you can see a Return command followed by your variable definitions. The Return command is not a filler; it has a specific effect on a script. Unlabeled code that follows it is unreachable and will never execute. As already stated above, you can rectify this by moving the variable definitions to the top of the script.
Last edited by mikeyww on 05 May 2024, 09:23, edited 1 time in total.
Joey5
Posts: 131
Joined: 19 May 2023, 05:25

Re: Script only works in isolation

05 May 2024, 09:23

Brilliant! TY. I rearranged it. TY.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: AlFlo, mamo691, TAC109 and 107 guests