It depends, if you want it to work for ALL healing items, then your best bet is a src edit.
However, if you only want to make a select few items have this effect, you can do so with script code.
Example of the latter:
This is the original Item for 607 (Yggdrasilberry).
{ Id: 607 AegisName: "Yggdrasilberry" Name: "Yggdrasil Berry" Type: 0 Buy: 5000 Weight: 300 BuyingStore: true Delay: 5000 Script: <" percentheal 100,100; ">},
This is the modified version to fit your request:
{ Id: 607 AegisName: "Yggdrasilberry" Name: "Yggdrasil Berry" Type: 0 Buy: 5000 Weight: 300 BuyingStore: true Delay: 5000 KeepAfterUse: true Script: <" if (readparam(Hp) == readparam(MaxHP) || readparam(Sp) == readparam(MaxSP)) { end; } else { percentheal 100,100; delitem 607,1; } ">},
You'll notice the KeepAfterUse: true
this will prevent the item from being deleted upon using it, even if they had maxHP/SP.
Then if they weren't, we heal them, and manually delete the item.