SD reports error in wrong place in a rather long script

This is a little weird so I’m made a movie to describe it.

Basically I have a command that calls a handler and tries to set the value of an undeclared variable to the result of the handler. Specifically the variable should be a list and the script is trying to append the hander result to that list.

When the script executes, the line that tries to change the value of an undefined variable should be selected when the error alert comes up. Instead, the line inside the handler that returns the handler’s result is selected as the error alert displays. This confused me because I was looking for an error inside the handler, but there wasn’t one.

I made a second, barebones test script to see if that would illustrate the problem, but it worked as expected (in the movie). Not sure what it is about my script that would cause this. I’m saving a copy as is in case you want to look at it.

Here’s the command in question.

--set linksTextTargets to {}
set the end of linksTextTargets to ReplaceLinksGetTargets(helpFilePath)

This variable “linksTextTargets” is not defined so there should be an error reported, it’s just getting reported in the wrong place (at the handler’s return line rather).

As you’ll see in the movie this is in debugging mode. If I turn off debugging mode the error is reported in the correct position. If I turn debugging mode back on, the problem reappears.

I can’t see your movie, but…

Script Debugger relies on AppleScript to tell it where an error happened — AppleScript returns stuff like the error number and message, as well as the range in the compiled script. And unfortunately there are cases where AppleScript returns the wrong range. This is particularly the case when an error happens inside code in a script library.

In cases where the range is obviously wrong when stepping, Script Debugger tries to make an educated guess. But other than that, SD is in AppleScript’s hands.

Interesting. In this case it’s only in debugging mode, whether stepping or running that the wrong line of the script is highlighted. With debugging off, it’s fine.