Applescript debounce

Hi,

I am trying to debounce a button that sends a midi signal using applescript and midipipe.

Essentially when I press the button it sends a particular midi message {178, 14, 127} and for the purposes of this example, I want it to only pass that message on once in a 10 second time frame. Meaning that pressing the button sends the message instantly, but then any subsequent presses within 10 seconds of the first press are ignored and not passed through.

here is the code I have written so far:

property tick : 1
property thismany : 1
on runme(message)
set y to 10
repeat
if (item 1 of message = 178) and (item 2 of message = 14) and (item 3 of message > 0) and (y = 10) then
return message – returns the message to midipipe to be passed on to Mainstage
end if
if y is less than or equal to tick then
set y to 0
exit repeat
end if
set y to (y - tick)
delay thismany
end repeat
end runme

My problem is that this is just delaying any subsequent presses for 10 seconds, so if I press the button three times quickly it sends one message instantly, then the next 10 seconds later, then in another 10 seconds etc.

Any help would be appreciated, I don’t know what I’m doing and do not have much coding knowledge.

Obviously I intend on reducing the timeframe of the debounce, but 10 seconds for now makes it easy to see if its working.

Cheers

Jeremy