Debugging a
if
statement
When you meet:
if [boolean expression] then [...] else [...] end if
select the boolean expression then press ⌘R. The Console will display true or false according to the result of the test. Then run the line(s) after then or those after else according to the boolean value.
Debugging a
repeat
loop
You can select a repeat [...] end repeat block and run it as a whole. Insert tracking execution commands to collect debugging information. For instance, if you think that the loop fails after a high number of iterations, you may want to set a counter (i) and insert a postit command like in the example below.
set i to 0
repeat with the_item in the_list
set i to 1 + i
-- lines to debug
postit i
end repeat
When the loop fails, the Message window will be displaying the latest loop index which worked correctly.
Now you can set manually the loop index to the relevant value:
set the_item to item n of the_list
n being the value you have identified as the one you want to inspect. Then, run line by line the core of the loop in order to identify which line is causing trouble.
If it is important to let the first iterations run normally before you start stepping into the nth iteration, simply insert (before the end repeat line) the following line:
if i is n - 1 then return
To run several iterations line by line (for instance, if you expect a failure after a small number of iterations), set a counter and simulate the loop like in the example below.
repeat with the_item in the_list -- do not run that line in the debug phase
set i to 0 -- initialize the counter once
set i to 1 + i -- before each iteration, run that line ...
set the_item to item i of the_list -- ... and that one
-- lines to debug line by line
end repeat -- do not run that line in the debug phase
Run first the line which initializes the counter, then step in the loop. For each iteration, run the two "loop simulation" lines, then run the core of the loop line by line (or, block by block), check the result in the Console. You do not need to care about properly ending the loop, since you will naturally trigger an Invalid index error when the counter's value exceeds the list's size.
|