Define global variables with inputbox, then insert with other macro Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
flw
Posts: 3
Joined: 14 Nov 2017, 04:00

Define global variables with inputbox, then insert with other macro

14 Nov 2017, 06:00

Hi,

recently I started to use autohotkey and it is fantastic. Now I have a new idea and as a beginner would like your input on how to write it correctly:

My idea is as follows:

Code: Select all

;Test
#1::
InputBox, variable1, Reference, Please enter a reference range,,,,,,,,%variable1% ;e.g., "900-000"
InputBox, variable2, Counter, Please enter a counter start,,,,,,,, ;e.g., "3"
;MsgBox, %variable1%%variable2% ; e.g., "900-0003"
return

#2::
SendInput {Backspace} %variable1%%variable2% ; e.g., "900-0003"
variable2++
return

1. How do I pass the variables between the two macros? I cannot transfer the documentation for global variables (https://autohotkey.com/docs/Functions.htm#Global) to my example.
2. As shown above, I would like to populate the Inputbox with the current string stored for variable1 - do I need to error-proof this somehow if variable1 may be empty or is it just empty then?
2b. Would it be possible to store the selected string as variable1, if any string is selected when #1 is pressed?

I am grateful for your suggestions. :D
User avatar
evilC
Posts: 4341
Joined: 27 Feb 2014, 12:30

Re: Define global variables with inputbox, then insert with other macro

14 Nov 2017, 06:36

It works exactly as you described?
The first time you hit #1, variable1 is empty, so the InputBox is empty.
The 2nd time you hit #1, the value that you entered the 1st time is pre-populated.
If you want to have a default value for variable1 the 1st time you run the script, then set the variable in the auto-execute section

Code: Select all

variable1 := 123
variable2 := 10
return

#1::
InputBox, variable1, Reference, Please enter a reference range,,,,,,,,%variable1% ;e.g., "900-000"
InputBox, variable2, Counter, Please enter a counter start,,,,,,,, ;e.g., "3"
;MsgBox, %variable1%%variable2% ; e.g., "900-0003"
return

#2::
SendInput {Backspace} %variable1%%variable2% ; e.g., "900-0003"
variable2++
return
flw
Posts: 3
Joined: 14 Nov 2017, 04:00

Re: Define global variables with inputbox, then insert with other macro

14 Nov 2017, 07:07

Yes, it works. It seems I overcomplicated it in my mind. Very intuitive in hindsight. Thank you.
But would it be possible to populate variable1 with the highlighted text if it is empty, before showing the Inputbox for the first time?

Example:

I start AHK.
Variable1 is empty.
I select the string "ABCD".
I press the first button.
The Inputbox (as described above) is shown, populated with "ABCD".
I enter "ABCDE".

...

I press the first button for the second time.
The Inputbox (as described above) is shown, populated with "ABCDE".
User avatar
evilC
Posts: 4341
Joined: 27 Feb 2014, 12:30

Re: Define global variables with inputbox, then insert with other macro

14 Nov 2017, 07:27

Code: Select all

variable1 := ""
variable2 := 10
return

#1::
if (variable1 == ""){
	Send ^c
	variable1 := clipboard
}
InputBox, variable1, Reference, Please enter a reference range,,,,,,,,%variable1% ;e.g., "900-000"
InputBox, variable2, Counter, Please enter a counter start,,,,,,,, ;e.g., "3"
;MsgBox, %variable1%%variable2% ; e.g., "900-0003"
return
flw
Posts: 3
Joined: 14 Nov 2017, 04:00

Re: Define global variables with inputbox, then insert with other macro  Topic is solved

14 Nov 2017, 09:10

Nice, thank you.

Here is my final code:

Code: Select all

variable2 := 1 

#1::
ClipSaved := ClipboardAll
Clipboard := ""
Send, ^c
ClipWait, 1
if (!ErrorLevel){
	AltCB2 = %Clipboard%
	variable1 := AltCB2 ;store clipboard into variable1
	AltCB2 := ;empty alternative clipboard
	StringRight, variable2, variable1, 3 ;if new string copied set variable2 to its last three digits
	}
Clipboard := ClipSaved
InputBox, variable1, Reference, Please enter a full reference range.,,,,,,,,%variable1% ;e.g., "900-000"
InputBox, variable2, Counter, Please enter a counter range to start with.,,,,,,,,%variable2% ;e.g., "3"
return

#2::
StringTrimRight, variable1trimmed, variable1, StrLen(variable2) ;trim variable1 by length of variable2, e.g., "900-00"
SendInput {Backspace}%variable1trimmed%%variable2% ; e.g., "900-003"
variable2++ ;add 1 to counter-variable variable2
return

Return to “Ask For Help”

Who is online

Users browsing this forum: Bad husband, Bing [Bot], Google [Bot], JoeWinograd, tm6464 and 24 guests