AHK GUI does not work - autohotkey

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.

Related

AHK suddenly requires return before } when i add GUI

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

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

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

autohot key using Text Box and a button

Need a little help with a text box and a button..
I have been trying to experiment with "text box" and a "button"..but am not able to do it... I need to take the "values" from the "textbox" field and append it to the button action. i.e., whatever the input is given by the user... need to append it to a "URL"
Example:
If the user is types in "login" as input in the text field, need to take this input and append it to "http://www.google.com/login"
Before action:
http://google.com
AfterAction:
http://google.com/login
Kindly help
Here is my Code:
Gui, Add, Edit, x162 y80 w140 h30 vUserInput, Type here
Gui, Add, Button, x182 y130 w100 h30 gAction, %"Action1" Action1:=http://www.google.om/
; Generated using SmartGUI Creator 4.0
Gui, Show, x127 y87 h379 w479, New GUI Window
Return
Action1:
Gui, Submit, NoHide
guiControlGet, txtVar,, UserInput
guiControl,, UserInput, %( txt++Action1 )
GuiClose:
ExitApp
return
Thanks and cheers.
Okay, you have a few misconceptions and a LOT of errors.
The use of := -vs- % is one of your misunderstandings.
Also, you neglected to put gAction1 as the goto label for your button. You had gAction, but there was no sub called Action.
Another problem is that you have not placed return at the end of your Action1 label.
That means that after the action is completed, the script will continue on to end the script...
You should really read the AutoHotkey Documentation! It will save you a lot of time and headache. Also, you should look at some simple examples.
However, try the script below. Whatever you type into the box, after you push the button, will be appended to the url.
;Now, it may be easier to define your re-usable variable here like this:
;myurl = http://www.google.com/
myurl := "http://www.google.com/" ;this is exactly same as the previous line
Gui, Add, Edit, x162 y80 w300 h30 r1 vUserInput, Type here
Gui, Add, Button, x182 y130 w100 h30 gAction1, Action1 ;The last parameter is JUST the text on the button
;If you wanted to use a variable for the name instead of straight text, you would do this:
;Gui, Add, Button, x182 y130 w100 h30 gAction, %myurl%
;But what you were trying to do is to *assign* a variable instead of *using* a variable - that is, you CAN'T use action1:=value as the variable name
Gui, Show, x127 y87 h379 w479, New GUI Window
Return
Action1: ;the name of this label is the same as the g-action of your button except without the "g" part.
Gui, Submit, NoHide
guiControlGet, UserInput ;you can simply use the edit control's v-name as the output variable
thisurl = %myurl%%UserInput%
guiControl,,UserInput, %thisurl% ;now set the url back to the control
return ;if you don't put return here, the script will continue on and run the GuiClose label...
GuiClose:
ExitApp
return ;you can have this, but you don't need it here since the script will have ended before it gets run anyway.