according to your toggle problem:
you perhaps want to try to let the editor do this work for you.
menu_add supports the option "-toggle", where you can add an expression to evaluate if the menuitem should be marked or not.
I personally use a seperated variable for this, so i can easly turn it on or of by setting the variable. To make it clear, here some code:
menu_add .YourMenu. "YourApplication" -cmd { package::function() } -toggle 'package::variable != 0'
so everytime you set 'package::variable' on some value different from zero the menuitem is marked.
setting back on zero removes the marking.
greetings
Andi