Using the Debugger
In the Editor there is a menu called 'Debug' with the following options
Step (F8)
This highlights the currently selected line, and then subsequently executes that line and moves to the next line in execution flow.
Step Over (Shift-F8)
This option is enabled if the current line contains a GoSub command or an If statement which contains a call to a subroutine. Step Over executes the subroutine in whole and moves the debug cursor to the next line.
Stop Debug (CTRL-F2)
Stops debugging and resets everything.
Trace
This displays a small dialog where you can set an interval. Then the debugger auto-steps at the specified frequency. You can set the interval as low as 0 seconds although if set to 0 seconds the debugger actually runs each line with a delay of 0.2 seconds between them in order to give you a chance to see what is happening and interrupt the script if needed. It is recommended to disable Refocus Windows (see below) when using Trace so that focus does not switch back to the debugger between each line.
Run
Runs the script without stepping, from the currently selected line. The script will run to the end or until the next breakpoint, from where the script can be stepped or run.
Insert Breakpoint
Inserts a breakpoint after the selected line. Simply inserts **BREAKPOINT** on the next line. If running the script using 'Run' from the Debug menu, execution will pause when this Breakpoint line is reached and the script can then be stepped from this point. As of version 10 multiple breakpoints can be set. At any time pressing Run will run the script to the next breakpoint from where the script can again be stepped or run.
Variable Breakpoints
Allows you to set one or more variable values on which script execution to break. Specify name=value pairs. E.g. to make the debugger pause when X=5 enter X=5 in the list. Then, when you run the script execution will pause when X=5 no matter where that happens. The script can then be stepped or continued from this point.
Run From Top
With this item enabled the script will always be run from the first line when it is started. With this option unchecked the script will be started from the line containing the cursor. This can be useful when debugging scripts and you only want to run a section of code.
Refocus Windows (check on or off)
Most of the time you want this checked on. Due to the nature of Macro Scheduler, and the fact that it is commonly used to automate other windows, most scripts will focus other apps and chop and change focus a lot. 'Refocus Windows' ensures that if a command causes focus to shift, focus will be set back to that window before executing each subsequent line so that (hopefully) that line will affect the correct app. Focus is returned to the editor after each line so that you can see what you're doing. Sometimes it is useful to turn this off ... if you're not doing any GUI scripting, or even at some point during a GUI script where you might have a loop, say, calculating some value but not actually requiring focus of anything. So you can switch it on and off during debug as required.
Show Watch List
Displays a list to the left hand side of the editor containing all current script variables and their values - updates as they change. This is the most useful debugging device as you can see the values of variables as they are set. By default the watch list shows the most recently modified variable at the top of the list. Or you can sort the watch list by right clicking and selecting "Sort". Right clicking on an item allows you to copy the line or just the variable value to the clipboard. You can also search for a variable or value by typing a value into the search bar at the top of the watch list.
Open Watch List on Debug
With this option enabled the Watch List will always open when the script is started.
Lock Watch List Position
As the script executes new variables are added to the watch list as they are created. Depending on how you have the Watch List sorted this will mean the position of variables within the watch list will change. E.g. new variables appearing at the top. If you want to keep your eye on a particular variable and not have it obscured when a new variable is added, enable Lock Watch List Position to prevent the Watch List from scrolling and keep the line you are interested in visible.
Debug Line Color
Allows you to change the color that is used to highlight the active script line.
Notes
Remember that stepping through a script will slow it down, since you will be pausing at each line. Since many scripts that automate windows applications need to be time-sensitive - to make sure things don't happen before apps are ready etc - the process of debugging may give the impression that the script is fine, when it actually needs delays and waits to be built in!
Using the Watch List
You will notice that variables in the watch list are preceded with a number, starting at 0. Unless you use LOCALVARS you will never see any other value. With LOCALVARS enabled this number changes depending on the scope level. E.g. a global variable called MyVar will be at level 0 while a local variable with the same name will be 1 or more. The higher number relates to nested level of the subroutine.
Arrays will show (Array) after them. Double click on array variables to see their element values.
You can modify a variable on the fly during debug before stepping or after reaching a breakpoint. Right click on a variable and select Modify... to change it's value.
You can also copy a line or value or sort the watch list using the right click menu.
Other Debug Methods
Other things you can do to help debug your script include:
·Use LogFiles - See Logging options
·Use MessageModal command to display diagnostic information on screen
·Use _DUMP_VARS variable to dump all variable values to the log file
·Use the Variable Explorer (Tools/Variable/Code Explorer or CTRL-ALT-V) to view all user variables and find out where they are created/modified. This also lists all subroutines and dialogs.
·Use the STEP_DELAY variable to slow the script down.
For an introduction on using the debugger watch this video:
http://www.mjtnet.com/demos/using_macro_schedulers_debugger.html