I noticed some surprising results of retrieving negatively indexed text items from text strings split with Applescript’s text item delimiters.
Each of the following cases results in the expected behavior of throwing error # -1728: Can’t get text item . of “x”
set theText to "x"
set AppleScript's text item delimiters to "" -- i.e., the empty string
theText's text item 2 --> error # -1728: Can't get text item 2 of "x"
theText's text item -2 --> error # -1728: Can't get text item -2 of "x"
set AppleScript's text item delimiters to "." -- or any non-empty string other than "x"
theText's text item 2 --> error # -1728: Can't get text item 2 of "x"
set AppleScript's text item delimiters to "x"
theText's text item 3 --> error # -1728: Can't get text item 3 of "x"
and now for the unexpected behavior:
set theText to "x"
set AppleScript's text item delimiters to "." -- or any non-empty string other than "x"
theText's text item -2 --> or text item -3, or text item -4, .: returns "x" without an error!
set AppleScript's text item delimiters to "x"
theText's text item -3 --> or text item -4, or text item -5, .: returns "x" without an error!
Why do seemingly out-of-range negative indexes not result in error # -1728? Why is this observed with a non-empty-string text item delimiter, but error # -1728 is observed with out-of-range negative indexes when the text item delimiter is the empty string?
I certainly seems to be a bug. (By the way, the result of the second example in your second script is “”, not “x”.) It only happens with text items and negative indices and only when none of the delimiters is “”. Your second script correctly errors on my Leopard system, but, with the latest Mavericks, returns text item 1 if the negative index is out of range.
Thank you for confirming the results. The bug is observed in Snow Leopard 10.6, Mountain Lion 10.8 (my system), and Mavericks 10.9, but not Lion 10.7.almost as strange as the bug itself.
Nigel, to add to the strangeness, the result of the second example was “”, as you pointed out, for my early tests, then morphed into “x” in my later tests. I was so surprised that I figured I had misread the earlier readings and thus submitted “x” as the result in the post. When I re-ran the tests today, it was back to “” again.
I do apologise. I was confusing the "L"s. The script correctly errors on my Leopard (10.5) system. The bug probably does occur in Lion too if it’s in Snow Leopard and Mountain Lion. I’ve corrected my post above.
I submitted this as a bug to Apple Bug Reporter and will update this thread with any information that comes in.
Addendum 2014-03-10: Apple responded that the submission duplicated a previously submitted bug report, whose status is listed as “open”. So Apple is well aware of and presumably working on the problem.