alexs1
For repeat behavior, think of each click as being the same as what would happen if you weren't using a multi-click script, but just had separate buttons for Mute and InstantMultiply. You're effectively pressing the Mute button first, then the InstantMultiply button, they're unrelated things that happened one after the other.
What's different about a script is that in the repeatCount == 2 block, it can assume that a Mute had been done previously and try to cancel it. This is possible with Undo but it's sensitive to the environment. If the Mute was quantized, and is happening far enough in the future that you get that third click in on time, then Undo should cancel the Mute event.
if repeatCount == 2 { Undo InstantMultiply}
The problem is that this is sensitive to there being quantization enabled for the Mute. If quantize was off, then there would be no Mute event since the Mute happened instantly, and the Undo would probably Undo the entire layer you were in the middle of recording. Undo works by taking future quantized events away one at a time and when there are no more events it undoes the layer.
So you could test for quantization like this:
if repeatCount == 2 {if quantize != :off Undo InstantMultiiply}
If quantize was indeed off it won't undo the Mute so you would have to add more logic to turn Mute off since it has already happened, probably this but I'd have to test it.
if repeatCount == 2 {if quantize != :off Undo else Mute InstantMultiply}
It's not 100% robust but should work most of the time. What would be better is something that could ask "do you have a mute scheduled and if so undo it" without having to make assumptions about quantize. Maybe Undo(:Mute) that does nothing if there isn't actually a Mute scheduled.
Depending on what you're doing on the earlier clicks, it may not be possible to use Undo to make it like it never happened. In those cases, you can use OnTimeout rather than OnRepeat to make decisions after all the clicks have come in, rather than while they are coming in.
function OnTimeout {
...same as what you started with...
}
The tradeoff of course is that there will be lag before anything happens, determined by the repeat timeout which is 400 milliseconds in your example. For some things that may not matter, but it isn't great if the second click does something that needs to be timed accurately.