You can check for valid unitids
I don't have WoW atm but something like this might work.
Code:
/cast [@target,targettarget=player] Spell1; [@target,party][@target,raid] Spell2
This "should" cast spell1 on your target if the target of target is yourself (player) and spell2 on your target if that target belongs to your party or raid.