1
votes

I am a newbie with AutoHotKey, and to this point I consider myself fortunate to have created a script to automate about 90% of the data entry in a window. I'm now trying to kick it up a notch and add a warning/caution GUI to my data entry script. I want to pause the running script and open a GUI that will alert me to look at what has already been entered, with one button to allow things to proceed if a particular entry looks OK, and another to dump out of both the GUI and the remainder of the script if the entry is incorrect.

Here's some code I came up with, mixed with some pseudocode to help show what I want to do.

(Previous data entry script executes to this point)
Stop the script
Gui, New
Gui, Add, Text, 'n Check Authorization Number to be sure it is A1234 (FY 15). ; Wraps text
Gui, Add, Button, Default, Continue
Gui, Add, Button, Quit
Gui, Show, IMPORTANT!
If Continue button is clicked
----Continue with script
If Abort button is clicked
----Close the GUI
----Exit the entire script (Resume where I left off with rest of the data entry script)

I've read the AHK Help file and am stumped about how to make these buttons work, as well as how to properly return from the GUI back to the script if I hit continue, or quit the whole thing if I spot a problem. I can tweak things like the size of the GUI and the button placement; what's most important is getting the GUI code right. Can someone help with the code? (The last programming class I took was in 2003, so I have forgotten a whole lot!)

1

1 Answers

9
votes

In your case, a GUI would be overkill. AHK (and many other languages at that) provide standard dialogs for simple user interaction called message boxes.
Message boxes in AHK can be displayed in two ways:

  1. MsgBox, This is a simple message. Please click "OK".
  2. MsgBox, 4, Attention, Here`, you can choose between "Yes" and "No".

In the second case, we declared an option which controls the buttons that are displayed. You can use IfMsgBox in order to detect what the user has clicked:

IfMsgBox, Yes
    MsgBox, 4, Really?, Did you really mean "Yes"?

Putting these pieces together, we can ask the user to decide if they want to continue, without the need to create a GUI, in a few lines:

; Option "1" is "OK/Cancel"
MsgBox, 1, IMPORTANT!,  Check Authorization Number to be sure it is A1234 (FY 15).
IfMsgBox, Cancel
{
    ExitApp
}
; do stuff

This code addresses each of your other problems, too:
A message box halts the current thread until the user dismisses the dialog or it times out (if you specifically declared a timeout).
To completely stop the execution of our script, we use ExitApp.