How do I make a macro that will /targetenemy when the enemy is in combat with me?

Enhance here. When I’m getting instant procs and generating maelstorm quickly, I can often spam spells (lightning bolt, chain lightning, lava burst, elemental blast). My problem is, unlike melee abilities, when my target dies, i have to tab over to cast another spell on my enemy group. This takes time causing less dps. I want to be able to hit my spell button and it automatically target an enemy before casting. I also want this enemy to only be one in combat with me, so i don’t accidently pull something close by that’s not in combat with my group. is this possible?

Targeting an enemy, sure. Targeting one that’s in combat with you, I don’t believe so. I don’t think /targetenemy or /startattack obey the tab targeting CVars.

That said, /startattack will target the nearest one.