AFAIK thats not doable - pretty much all these sort of queries were removed from lua/scripts/macros with patch 2.0

e.g. There's no way to tell what buffs are on a player or mob using macros

But as welwyn pointed out it is possible to cast a spell based upon certain target conditions so it depends on exactly what you want to do...