make it an option in the attack menu with a text command (/automaticrangeattack /ara)

and while under this mode show the player with ranged weapon in hands instead of melee weapons
...