SD var pane ≠ AS events when indirectly changing mutable var

I’ve been encountering odd behavior with list variables. This is a case of seeming mismatch between SD (7.0.12) variable pane and Apple Events. AS returns the same event log. I restarted and got the same behavior. (Sorry that I can’t remember how to post code in the forum and the FAQ link now references rules of behavior.)

use AppleScript version “2.4” – Yosemite (10.10) or later

on run
	local theList
	set theList to {1, 2, 3}
	log theList -- event = (*1, 2, 3*) SD var pane = {1, 2, 3}
	log test(theList) -- event = (*4*) SD var pane = {1, 2, 3}
	log theList -- (*4, 2, 3*) SD var pane = {1, 2, 3}
end run

on test(l)
	set item 1 of l to 4
end test

I get similar behavior with setting list item in same handler.

on run
	local theList, l
	set theList to {1, 2, 3}
	set l to theList
	log theList -- event = (*1, 2, 3*) SD var pane = {1, 2, 3}
	log l -- (*1, 2, 3*) SD var pane = {1, 2, 3}
	set item 1 of theList to 4
	log theList -- (*4, 2, 3*) SD var pane = {4, 2, 3}
	log l -- (*4, 2, 3*) SD var pane = {1, 2, 3}
end run

This is a long standing limitation of Script Debugger’s method of tracking local variables.

Because AppleScrip[t provides no infrastructure for debugging, Script Debugger users a technique called instrumentation to track your script’s execution and state. This involves inserting additional hidden statements into your code that allow Script Debugger to track you script’s progress and sample the value of local variables.

For performance, efficiency and script length reasons, Script Debugger only samples variables that have been altered by the preceding statement.

In your example, the theList and l variables refer to the same underlying list value. When your code alters theList, Script Debugger does not re-sample l and so Script Debugger’s variable display falls out of sync.

We provide a means of working around this problem. You can use a special AppleScript comment to sample variables explicitly: --= varName, varName ....

I can alter your example like this to correct the problem:

on run
	local theList, l
	set theList to {1, 2, 3}
	set l to theList
	log theList -- event = (*1, 2, 3*) SD var pane = {1, 2, 3}
	log l -- (*1, 2, 3*) SD var pane = {1, 2, 3}
	set item 1 of theList to 4
	--= l
	log theList -- (*4, 2, 3*) SD var pane = {4, 2, 3}
	log l -- (*4, 2, 3*) SD var pane = {1, 2, 3}
end run

Of course this isn’t a perfect solution because you need to somehow know that Script Debugger isn’t showing you correct values. This problem is mostly limited to multiple variables referencing the same underlying value.

Note that this issue does not effect global variables and properties because AppleScript provides a reliable means of sampling all global variables to Script Debugger.

Thanks Mark. I suggest that you update your SD Help page for the variables pane. I looked there before I posted and didn’t see a caveat.