Viewing Local Variables in Script Debugger
The issue of how to view local variables (variables declared within an AppleScript handler) in Script Debugger keeps coming up. This problem has been an issue ever since Script Debugger 2.0 when local variable display was introduced. This issue surfaced again recently on the AppleScript-Users mailing list:
Hello, how can I see the value of y in the variables pane of Script Debugger when I run this simple example script in debug mode: set x to 3 beeping(x) on beeping(x) --local y set y to 5 beep x delay 2 beep y end beeping Bert.
Script Debugger requires all local variables be explicitly declared before they are displayed in the variables browser. AppleScript allows you to explicitly declare locals in several ways:
- explicitly using the ‘local’ statement
- as the loop variable in a ‘repeat with’ statement
- as a parameter to a handler
- as a parameter to an ‘on error’ statement
The problem arrises when a variable is declared implicitly through an assignment within a handler. In this instance, Script Debugger’s parser is not able to detect the new variable (y), and thus fails to display it in the variables browser.
The solution is the uncomment the ‘local y’ statement.
You can find additional information in the Script Debugger Help Book by searching for ‘local’ from the Help menu.
The AppleScript runtime does not provide public debugging hooks. As a result, Script Debugger must resort to a series of hacks to implement step-wise debugging and variable display. One of the limitations of this approach to AppleScript debugging is that Script Debugger must detect the names of all local variables at compile-time. AppleScript’s support for multi-word identifiers in dictionaries makes parsing AppleScript source a very problematic thing to do, and makes detecting local variables without knowing AppleScript’s internal state impossible.