AHK suddenly requires return before } when i add GUI - autohotkey

When making a small project i added a GUI and it suddenly requires return?
i want to have it ask what a folder should be named then create it
it was working, but then i added GUI for what you want to download and it requires return
I couldn't find anything else on this topic anywhere else
Working code:
if !FileExist(%UserInput%) {
MsgBox, 0, Serkket Servers, Server will build files, press ok to continue `n Server will open when done
FileCreateDir, %UserInput%
}
Not Working code:
if !FileExist(%UserInput%) {
Gui, New,, Server Software
Gui, Color, 707070
Gui, Add, DropDownList, vServVers, 1.16.5|1.16.4|1.16.3|1.16.2|1.16.1|1.15.2|1.15.1|1.15|1.14.4|1.14.3|1.14.2|1.14.1|1.14|1.13.2|1.13.1|1.13|1.12.2|1.12.1|1.12|1.11.2|1.11.1|1.11|1.10.2|1.9.4|1.9.2|1.9|1.8.8|1.8.3|1.8
Gui, Add, Button, Default w80, OK
Gui, +Resize +MinSize480x240
Gui, Show
Pause, On
ButtonOK:
Gui, Submit
MsgBox, 0, Serkket Servers, Server will build files, press ok to continue `n Server will open when done
FileCreateDir, %UserInput%
}

The return in this case is required because you have a ButtonOK: label that hasn't been properly closed off to form it's subroutine. As this return is needed, you will need to move the label elsewhere in your code, somewhere outside the current section. If this code is happening at the top of your file, you will need to move it outside the autorun segment.
To aid with this, we can give the GUI a label which is done using Gui, MyLabel:New. This allows AHK to work with different GUIs at the same time, but it also makes it easier to deal with the default window event handlers by changing GuiClose to MyLabelGuiClose.
This updates your conditional statement to:
if !FileExist(%UserInput%) {
Gui, SelectServers:New, +Resize +MinSize480x240 +HwndSelectServersHwnd, Server Software
Gui, SelectServers:Color, 707070
Gui, SelectServers:Add, DropDownList, vServVers, 1.16.5|1.16.4|1.16.3|1.16.2|1.16.1|1.15.2|1.15.1|1.15|1.14.4|1.14.3|1.14.2|1.14.1|1.14|1.13.2|1.13.1|1.13|1.12.2|1.12.1|1.12|1.11.2|1.11.1|1.11|1.10.2|1.9.4|1.9.2|1.9|1.8.8|1.8.3|1.8
Gui, SelectServers:Add, Button, Default w80 gSelectServersButtonOK, OK ; <- don't forget to connect the handler
Gui, SelectServers:Show
WinWaitClose, ahk_id %SelectServersHwnd% ; <- This is a better choice than "Pause, On"
}
and this can be added anywhere outside of the autorun section (like at the bottom of the file)
return ; <- makes sure the auto run section has ended (only needed once)
SelectServersButtonOK:
Gui, SelectServers:Submit
MsgBox, 0, Serkket Servers, Server will build files, press ok to continue `n Server will open when done
FileCreateDir, %UserInput%
Gui, SelectServers:Destroy
return
SelectServersGuiClose:
SelectServersGuiEscape:
; TODO: Handle cancelling selection GUI
; e.g. ExitApp, cancel folder creation, etc.
Gui, SelectServers:Destroy
return

Related

Close GUI but continue script in AHK

I'm trying to have a gui close but leave the script running. This is because I want to do other actions if the user chooses to escape the GUI by either hitting esc or simply clicking the 'X' in the upper right. I don't understand how I would leave the script running but close the gui. GUI close doesn't seem to do anything when clicking esc or the X. I've scanned through the GUI docs and cannot figure it out. They always run exitapp, but I'm not ready to exitapp, I need to do other things.
Gui, Add, Text, ,To cancel, press ESCAPE or close this window.
Gui, Show, w320 h80, Downloads
GuiClose:
GuiEscape:
; Continue on to do other things here!!!!
WinActivate ahk_exe notepad++.exe
; do things...
exitapp
What you're describing sounds like what I wanted to do, too. It was a lot of slogging and piecemealing but I think what you are looking for is Gui, Cancel Take a look at the code below and see if that doesn't solve your problem.
It doesn't use ExitApp and it doesn't destroy the window if you want to pop it up later. You'll have to figure how to place the rest of your code, but I believe this is what you are asking.
;Set the GUI window
Gui, Add, Text,, Hit Escape to Clear window when it is active
#F9:: ;show the window with WindowsKey-F9
Gui, Show,
return
;set the escape key to clear GUI window
GuiEscape: ; Note: single colon here, not double
Gui, Cancel
Return
It's a little trickier if you have multiple GUI windows. You must name each:
;Set the two GUI windows. In example, First is labeled as FirstGUI and second as SecondGUI. Notice how you separate with single colon.
Gui, FirstGUI:Add, Text,, Hit Escape to Clear FirstGUI window when it is active
Gui, SecondGUI:Add, Text,, Hit Escape to Clear SecondGUI window when it is active
#F9:: ;show the two windows ("xCenter y700" is used to prevent the windows from stacking.)
Gui, FirstGUI:Show, xCenter y700, Window Title of First GUI
Gui, SecondGUI:Show, xCenter y900, Window Title of Second GUI
return
;set the escape key to clear for each Gui independently.
FirstGUIGuiEscape: ; Note that you must add the Gui Name (FirstGUI) to the beginning of the "GuiEscape:" command and also name it in the following:
Gui, FirstGUI:Cancel
Return
SecondGUIGuiEscape: ; And here you must add SecondGUI as noted above.
Gui, SecondGUI:Cancel
Return
Answering a little late but hope this works for you or someone else!
They assume in the documentation that by clicking x, you'd want the script to close.
So they show ExitApp as an example.
If you don't want to do that though, of course no need to do it.
I think what you're after is destroying the gui:
GuiClose:
GuiEscape:
Gui, Destroy
return

How to make it so you could change the hotkey in the GUI? - AutoHotkey

Code:
Gui, Add, Text,, ------------------------------------------Key Delay------------------------------------------
Gui, Add, Edit, w300 vKeyDelay, 100
Gui, Add, Text,, ------------------------------------------Key Input------------------------------------------
Gui, Add, Edit, R10 w300 vKeyPlayer
Gui, Add, Text,, ------------------------------------------Key Start------------------------------------------
Gui, Add, Edit, w300 vStartKey, F2
Gui, Show
F2::
!F2::
Gui, Submit, Nohide
SetKeyDelay, %KeyDelay%
Send, %KeyPlayer%
return
GuiClose:
ExitApp
The start key is set to F2, i want to make it so people are able to change it to whatever, (F1, F2, F3, A, B, C, 1-10, etc)
How to make it so you could change the hotkey in the gui?
You'd use the Hotkey command to create a hotkey during runtime.
And to choose the hotkey in the gui, the easiest (but not best) option would be the hotkey control.
It's surely the easiest one that's also convenient for the end user, but it doesn't support anything beyond just regular hotkeys. For better approaches, you'd need a custom one.
This was the first custom one I found with a Google search. Haven't used it myself, but it might be good stuff.
A very easy, but powerful, custom one you could also use is just a Edit control. Works very well if you expect your end users to be smart enough to type stuff like !F1, +#k, d & o, or whatever.
Anyway, I'll demonstrate here the usage of the built in hotkey control. Stop reading now if you want to figure it out yourself.
First, create the gui and associate a variable and a g-label to the hotkey control.
Though, I'm going to use a function instead of a label, I don't like writing legacy AHK.
Gui, Add, Hotkey, % "x50 y25 w90 h30 vChosenHotkey gHotkeyChanged"
Gui, Show, % "w200 h100"
Return
Then the g-label HotkeyChanged needs to be defined, and I'll use a function instead of a label, as said above.
HotkeyChanged()
{
global ChosenHotkey
Gui, Submit, NoHide
Hotkey, % ChosenHotkey, MyHotkey, On
}
And when using a function, you have to worry about scopes, which is why global ChosenHotkey is specified.
There I'm telling the function that I'll use a variable defined outside of its scope.
If scopes are something you don't yet know of, and don't yet want to to learn about them, you can write legacy AHK and use a label and forget about all this.
To learn about scopes in programming in general, you can probably find something good from Google.
And to learn about them in AHK specifically, I have a previous answer about them here and here's the relevant documentation page.
Then I get the make the script update its associated variables with Gui, Submit, you already seem to know about this.
And then I get to the Hotkey command.
First parameter takes the hotkey to use, that's stored in the ChosenHotkey variable.
Second parameter a label/function name, or a function object. I'll use a function name MyHotkey.
And in the third parameter On is specified to turn the hotkey on and possibly replace any previous any previous hotkey that was in its place.
Then the function or label MyHotkey needs to be defined:
MyHotkey()
{
MsgBox, % "Hotkey pressed!"
}
And that's it.
If you want to save the previously used hotkey and then use it again when the script is restarted, there are many options all of which basically just boil down the idea of saving the hotkey to some file.
Here's the full script:
Gui, Add, Hotkey, % "x50 y25 w90 h30 vChosenHotkey gHotkeyChanged"
Gui, Show, % "w200 h100"
return
HotkeyChanged()
{
global ChosenHotkey
Gui, Submit, NoHide
Hotkey, % ChosenHotkey, MyHotkey, On
}
MyHotkey()
{
MsgBox, % "Hotkey pressed!"
}
GuiClose()
{
ExitApp
}

Issues with AutoHotKey symbol pasting from GUI

I am new to AHK, and am trying to make a script that opens a GUI with a short list of commonly-used symbols that can be chosen, and then be automatically be pasted.
My code so far:
!+q::
Gui, Add, ListBox, w100 h100, vSymbolChoice, ™|©|°|π|☭|☢|⚠|ツ|•|Ω
Gui, Add, Button, Default, Submit
Gui, Add, Button, default, Cancel
Gui, Show
return
ButtonSubmit:
Gui, Submit
Sleep, 1000
Send, %SymbolChoice%
Gui, Destroy
ButtonCancel:
Gui, Destroy
It creates the GUI and the ListBox but doesn't paste the symbol when I have it selcted and press submit.
Also, is there a better way to detect if a text field is selected than just waiting a second and hoping the user selected the field in that time?
; auto-execute section
; create and show the Gui
Gui, Add, ListBox, w100 h130 vSymbolChoice, ™|©|°|π|☭|☢|⚠|ツ|•|Ω
Gui, Add, Button, Default, Submit
Gui, Add, Button,, Hide ; you can't have two default buttons on a Gui
Gui, Show
return
; Press Alt+Shift+Q to show the hidden Gui after ButtonSubmit or ButtonHide
!+q:: Gui, Show
ButtonSubmit:
GuiControlGet, SymbolChoice ; get the control's contents stored in the variable SymbolChoice (retrieves the ListBox's current selection)
Gui, Submit ; saves the contents of this control to its associated variable SymbolChoice
SendInput, %SymbolChoice%
return
; Hide the Gui
ButtonHide:
Gui, hide
return
; Press ESC or close the Gui to terminate the script
GuiClose:
Esc:: ExitApp
For details see GUI in the documentation.

Paste into custom GUI with AHK

I have been working on this for a while and I have no idea how to fix this issue. I want to create a custom GUI in AutoHotKey (AHK), I would post ont he AHK Forums but I haven't been able to get my account to work so I am posting here (sorry if this is the wrong place). The ideal state is that I can paste in a list of indiscriminate length from a list, it is almost always return delimited, see the picture below. I would be happy with pasting 10 items in. I have built the GUI but I can not paste the values in with the shortcut Ctrl+v. All that happens is the first value goes into the first cell and I cannot figure out how to get the rest to paste in.
I need to be able to read the values into an array in the AHK when I click continue. Thanks for your help in advance. Below is my code to create the GUI.
Gui, Add, Text,, Please add the List that you want (10 Max at once)
Gui Add, Edit, vButton1,
Gui Add, Edit, vButton2,
Gui Add, Edit, vButton3,
Gui Add, Edit, vButton4,
Gui Add, Edit, vButton5,
Gui Add, Edit, vButton6,
Gui Add, Edit, vButton7,
Gui Add, Edit, vButton8,
Gui Add, Edit, vButton9,
Gui Add, Edit, vButton0,
Gui Add, Button, x200 y270 w88 h26 vButton02 gGoCont, Continue
Gui Add, Button, x290 y270 w88 h26 vButton03 gGoQuit, Cancel
Gui Show
return
GoCont:
{
MsgBox %Button1%
MsgBox %Button2%
}
return
GoQuit:
Gui Destroy
return
If you can stand a txt file with one name per line, called "names.txt" in the same folder as your ahk script, try something like this:
Add this to the top (it reads in your names.txt file one line at a time):
Loop, Read, names.txt
x%A_Index% := A_LoopReadLine
START EDIT (per comments):
Alternatively, if you already copied to the clipboard the several names from a spreadsheet or website table or other list, then put it this way:
Loop, parse, Clipboard, `n, `r
x%A_Index% := A_LoopField
Either way,
END EDIT
Then, replace all 10 of your edit box lines with these two lines:
Loop, 10 ; or more?
Gui Add, Edit, vButton%A_Index%, % x%A_Index%
The rest is just as you had it.
Let us know, Have fun,
Big thanks to #PGilm
Gui, PasteGUI:Add, Text,, Please add the Names that you want to Process.
Counter := 0
Loop, parse, Clipboard, `n, `r
{
x%A_Index% := A_LoopField
Counter++
}
Counter--
Loop, %Counter% ; Dynamic List length
Gui PasteGUI:Add, Edit, vButton%A_Index%, % x%A_Index%
Gui PasteGUI:Add, Button, x200 y270 w88 h26 vButton02 gGoCont Default, Continue
Gui PasteGUI:Add, Button, x290 y270 w88 h26 vButton03 gGoQuit, Cancel
Gui, PasteGUI:Show
}
Return
GoCont:
{
Loop, %Counter%
{
CODE TO PROCESS MY EACH NAME
}
MsgBox Done!
Gui Destroy
}
Return
GoQuit:
Gui Destroy
Return
Lastly if I want to add a keyboard shortcut to work then I mapped in one where I put the below line at the top of the code
PasteIn:
{
And then close the bracket at the end of the code and then add the shortcut. (the below can be added to the bottom of the code to work) this uses the Ctrl+v keyboard shortcut.
}
^v:: GoTo, PasteIn

AHK GUI does not work

I'm using the following AHK script to pop up screen with checkboxes. Idea is that when I check on the boxes it should open a folder. It works for the first one, but when I check the second one nothing happens. Any ideas whats wrong with my code
Gui, Add, Checkbox, vOpen, Open Bedrijfsbureau
Gui, Add, Checkbox, vOpen2, Open
Gui, Add, Button, Default gButtonGo, Go
Gui, Show, w500 h300, Products
Return
ButtonGo:
Gui, Submit
Gui, Destroy
If Open = 1
Run "X:\SSC_HR\SENS\Bedrijfsbureau"
Return
If Open2 = 1
Run "X:\SSC_HR\SENS\Bedrijfsbureau\Dimensioneren"
Return
GUIClose:
Gui, Destroy
When you use an if statement without curly brackets, only the following line is read by the if statement.
That means that when you do this:
If Open = 1
Run "X:\SSC_HR\SENS\Bedrijfsbureau"
Return
...the program ends. The if statement does the run command and the return gets processed no matter what.
So, try this, instead, if you want multiple lines:
If(Open = 1){
Run "X:\SSC_HR\SENS\Bedrijfsbureau"
Return
}
Also, nothing will happen anyway! First, when you check the box, you have no sub to handle the checkbox. In other words, when you click the checkbox, nothing is supposed to happen. So, I suppose that you only want things to happen when you push the "Go" button.
That's great, but your go button does two things: it grabs the value of the checkbox, then it exits the GUI ----> THIS IS WHY YOUR PROGRAM DOESN'T WORK.
You want to do this, instead:
Gui, Add, Checkbox, vOpen, Open Bedrijfsbureau
Gui, Add, Checkbox, vOpen2, Open
Gui, Add, Button, Default gButtonGo, Go
Gui, Show, w500 h300, Products
Return
ButtonGo:
;fetch the value of the checkbox variables
Gui, Submit
If Open = 1
Run "X:\SSC_HR\SENS\Bedrijfsbureau"
If Open2 = 1
Run "X:\SSC_HR\SENS\Bedrijfsbureau\Dimensioneren"
;Now, if you want the gui to close after the actions are complete, then uncomment the next line - if you want the gui to remain, then don't uncomment it.
;Gui, Destroy
RETURN
GUIClose:
Gui, Destroy
Finally, in this scenario, you can execute either one or both of the checkbox commands. If you only want one or the other, you should use radio buttons instead.
For a hotkey to run the primary function:
#x::
gosub, ButtonGo
return
If you want the hotkey to open the GUI, then you need to wrap the GUI in a gosub and alter the hotkey's command.