InputBox with Variable Loop Amount Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Zedcars
Posts: 11
Joined: 17 Sep 2021, 07:50

InputBox with Variable Loop Amount

Post by Zedcars » 29 Oct 2021, 04:01

Hello,

I’m attempting to loop some code. The loop count needs to be different each time and should be defined by the number entered in the InputBox. When I run the following code I get the error at the bottom. I’ve tried all kinds of variations but obviously I’m missing something basic. I’m not sure where I’m going wrong. Any feedback would be greatly appreciated.

Code: Select all

^/::
 
{
 
IB.Value := 0
 
IB := InputBox("How many labels do you need to print?", "Number of Labels", "w220 h150")
if IB.Result = "Cancel"
    MsgBox "You entered '" IB.Value "' but then cancelled."
else
 
Loop %IB.Value%
{
SendInput "Down"
}
}
0AA39579-44B4-4553-8B17-930419B4AEE9.png
0AA39579-44B4-4553-8B17-930419B4AEE9.png (17.11 KiB) Viewed 2529 times

safetycar
Posts: 435
Joined: 12 Aug 2017, 04:27

Re: InputBox with Variable Loop Amount

Post by safetycar » 29 Oct 2021, 08:11

If you wanted to set a default value with the line 5, then that's not the way. There's a "Default" parameter described in the documentation for that.
The IB is only set at line 7, so you can't manipulate it before that.

Zedcars
Posts: 11
Joined: 17 Sep 2021, 07:50

Re: InputBox with Variable Loop Amount

Post by Zedcars » 29 Oct 2021, 11:15

safetycar wrote:
29 Oct 2021, 08:11
If you wanted to set a default value with the line 5, then that's not the way. There's a "Default" parameter described in the documentation for that.
The IB is only set at line 7, so you can't manipulate it before that.
Hi,

Thank you for your reply.

I thought I had to initialise a variable before I can store a value to it. That’s the only reason I put that line in there. It doesn’t seem to want to accept the IB.Value as a loop count.

safetycar
Posts: 435
Joined: 12 Aug 2017, 04:27

Re: InputBox with Variable Loop Amount

Post by safetycar » 29 Oct 2021, 11:48

I'm not sure if I can follow where you're thoughts are coming from. I'll try with some general hints.
What you're accessing with IB.Value is not a normal variable. It's a property of IB, which is an object.
If anything it would make more sense to initialize IB (IB:={} I guess), but not so much here since anything you put is going to be overwritten later with IB := InputBox....
Sometimes you are required to initialize a variable when it's going to be passed as parameter, but not for the output.

Zedcars
Posts: 11
Joined: 17 Sep 2021, 07:50

Re: InputBox with Variable Loop Amount

Post by Zedcars » 29 Oct 2021, 12:43

safetycar wrote:
29 Oct 2021, 11:48
I'm not sure if I can follow where you're thoughts are coming from.
My thoughts on initialising a variable came from this post:

viewtopic.php?p=244113#p244113

When my initial attempts to make my code work failed I searched for help on variables which led me to that post so I added line 5. But it still didn’t do what I wanted which is why I posted here.
safetycar wrote: I'll try with some general hints.
What you're accessing with IB.Value is not a normal variable. It's a property of IB, which is an object.
If anything it would make more sense to initialize IB (IB:={} I guess), but not so much here since anything you put is going to be overwritten later with IB := InputBox....
Sometimes you are required to initialize a variable when it's going to be passed as parameter, but not for the output.
I see. From what you have said it sounds like line 5 is not required then. Seems like I should have left that out to begin with.

The first part of the InputBox code seems fine (it would since it’s from the example in the documentation). It throws up an InputBox and I can put in a numerical value. The next part is where I’m falling down. Why is it not passing that numerical value onto the Loop to tell it to loop for the number I’ve entered?

(I found some old code that works under AHK v1 but I’m unable to run that as my work AV has blocked the software.)

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: InputBox with Variable Loop Amount  Topic is solved

Post by swagfag » 29 Oct 2021, 13:19

delete

Code: Select all

IB.Value := 0
the .Value property is already set/would be overwritten if you had set it, when the InputBox() returns

Code: Select all

Loop %IB.Value%
delete these % signs. instead check whether .Value contains anything numberlike

safetycar
Posts: 435
Joined: 12 Aug 2017, 04:27

Re: InputBox with Variable Loop Amount

Post by safetycar » 29 Oct 2021, 13:40

Zedcars wrote:
29 Oct 2021, 12:43
My thoughts on initialising a variable came from this post:
viewtopic.php?p=244113#p244113
I'd put that in a different way. That was because of toggle:=!toggle. When the script tried to interpret !toggle for the first time the toggle variable didn't had a value yet, and warnings appeared because of that. And so, the fix was putting the initial value before finding the negation.

For the rest @swagfag has given some answers.

Zedcars
Posts: 11
Joined: 17 Sep 2021, 07:50

Re: InputBox with Variable Loop Amount

Post by Zedcars » 30 Oct 2021, 11:29

swagfag wrote:
29 Oct 2021, 13:19
delete

Code: Select all

IB.Value := 0
the .Value property is already set/would be overwritten if you had set it, when the InputBox() returns

Code: Select all

Loop %IB.Value%
delete these % signs. instead check whether .Value contains anything numberlike
Thanks so much! It is now working.

Funny thing is I had tried as you suggest but my inexperience and lack of confidence with it caused me to think it was not. The reason I had assumed it not to be working was because I had messed up on the code within the loop; I had forgotten to add the {} around "Down" in line 14 so all it was doing was sending "Down" as text rather than as a keyboard command! The window I was testing it in was not accepting text input so I had not realised.

Anyway, thanks again. I've learnt some more basics.

Zedcars
Posts: 11
Joined: 17 Sep 2021, 07:50

Re: InputBox with Variable Loop Amount

Post by Zedcars » 30 Oct 2021, 11:33

safetycar wrote:
29 Oct 2021, 13:40
Zedcars wrote:
29 Oct 2021, 12:43
My thoughts on initialising a variable came from this post:
viewtopic.php?p=244113#p244113
I'd put that in a different way. That was because of toggle:=!toggle. When the script tried to interpret !toggle for the first time the toggle variable didn't had a value yet, and warnings appeared because of that. And so, the fix was putting the initial value before finding the negation.

For the rest @swagfag has given some answers.
I have it working now. Please see above. Thanks very much for your help with this. I'm learning slowly. I'm currently trying to replace my reliance on mouse clicks and it seems to be making my code run a bit quicker.

Kind regards.

Rospetal
Posts: 1
Joined: 08 Jun 2023, 19:35

Re: InputBox with Variable Loop Amount

Post by Rospetal » 08 Jun 2023, 19:50

@Zedcars

I'm sorry, it's been more or less 2 years, but I still can't find the correct code and I need it. Do you by chance still have the correct code?

I get an error when I try to fix it.
(It's funny, now the inexperienced one is me)

User avatar
mikeyww
Posts: 26972
Joined: 09 Sep 2014, 18:38

Re: InputBox with Variable Loop Amount

Post by mikeyww » 08 Jun 2023, 20:15

Code: Select all

#Requires AutoHotkey v2.0

^/:: {
 If 'OK' = (IB := InputBox('How many labels do you need to print?', 'Number of Labels', 'w300 h100')).Result
  Send '{Down ' IB.Value '}'
}

Post Reply

Return to “Ask for Help (v2)”