Script to change audio balance based in jack sense!

Hi! I would like to no if it is possible to right a Script with base in my “Jack sense”?

I need a script that change my audio balance to right when I plug in my headphones and then back to “center” when I unplug.