0

I'm using midi messages to trigger Applescript commands via MidiPipe. Midi messages contains 3 items and I want to trigger a command when the value of the third item increments +1

if (item 1 of message = 176) then 
    if (item 2 of message = 97) then
        if (item 3 of message + 1) then -- this doesn't work 
            tell application "System Events"
                key code 126

It works when the value I want is fixed but I don't know how to detect positive or negative increments (range 0-127)

1 Answers1

0

AppleScript doesn't have anything automatic like that. It would be up to you to keep track of what the previous value was, compare/update to what it is now, and take the appropriate action. You don't mention how the script is called or any state you are keeping, but your snippet would be something like:

property previous : missing value -- keep track of the previous value
if previous is missing value then set previous to item 3 of message -- initial value

-- other stuff

if item 1 of message = 176 then
    if item 2 of message = 97 then
        set change to (item 3 of message) - previous -- get any difference
        if change < 0 then set change to -change -- absolute value
        if change is not 0 and change = 1 then -- or whatever threshold comparison
            set previous to item 3 of message -- update
            tell application "System Events"
                key code 126
                -- other stuff
            end tell
        end if
    end if
end if

I don't have MidiPipe to test, but if the script won't keep persistent properties, you will need to do something like read from a file or use NSUserDefaults to keep the value of the previous variable between runs.

red_menace
  • 3,162
  • 2
  • 10
  • 18