osascript is not allowed assistive access. (-1728)
If you run AppleScript code via osascript in Terminal, then you need to add Terminal to System Preferences > Security &amp; Privacy > Privacy > Accessibility, to allow it assistive access.



I took your code, saved it to a file named testcode and made it executable using chmod. I then ran it in Terminal and received the following error:

./testcode:157:286: execution error: System Events got an error: osascript is not allowed assistive access. (-1728)


I then added Terminal to System Preferences > Security &amp; Privacy > Privacy > Accessibility and ran it again. It then processed the script properly and returned:

radio button Login Items of tab group 1 of window Users &amp; Groups of application process System Preferences


It of course opened the System Preferences to that target.