I have an update. The script works as expected on my M1 MacBook Air, not on the pro. I heard from the script developer and his assumption is that it's the Java scripting engine. He referred me to the PI developers for a solution.
I think I just have to use the Air just for the script and my Pro for everything else.
Another curious observation is that if I quit PI on the PRO and restart it, then I use the script, it works fine once, then for all the subsequent times the results are unpredictable.