That works, thank you.
With this out of the way, I was able to test the other code snippet for bonus effects when removing negative states. What I have is this:
<State Negative Category Remove: All>
<JS Pre-Apply>
target.gainTp(target.statesByCount("Negative").length * 5);
</JS Pre-Apply>
Which, unfortunately, doesn't work. Do you have an idea why?
Ideally, the item would only generate TP for each state actually removed, not just counted. To be honest, I don't have any plans of including states of the 'Negative' category that can't be removed with this item, but it's better to have too much design space than too little, right?

So, should you have an idea how to implement this, that would be even better.