The reason that the clear target function is important here is because it more or less circumvents the bolded rule. The script itself is not actually deciding whether you cast a spell, it’s deciding whether you retain your target. The macro folloing the script just doesn’t have anything to cast the spell on depending on the result of the script.
Similarly, this one is just setting a temporary/local variable and then polling through unitbuffs a set number of times and then updating the variable with the output and then either dropping target or not depending on a specific condition. The condition is not specifically casting any spells, it’s just preventing the rest of your macro from running because it made you drop target before that line has a chance to execute.
It is just basic coding using a “legal” condition and function to circumvent things that you’re not supposed to be able to do with scripts. The end result is that your character presses its kick button and it either kicks something or not depending on if the target is casting.