Can I set a global variable using this applescript?

on runme(message)

if (item 1 of message = 145) then
    set x to item 2 of message
else if (item 1 of message = 144) then
    set y to item 2 of message
end if
if (item 1 of message = 145) then
    return message
else
    set y to x * 8
    return {item 1 of message, y, item 3 of message}
end if

end runme

I am a complete newbie to Applescript. I receive messages MIDI messages (message). They take the form of three numbers (IE: 145, 0, 127)

What I need to do is listen to the MIDI note number starting at 145, and then look at it "point 2. Then I need to multiply this by 8 and save this as element 2 of the MIDI note number, starting at 144.

There will be a few notes starting with 144 for each note with 145. Therefore, I need to save this variable until note 145 appears.

The problem is that I think this script runs fresh every time a MIDI note passes through it? I need to somehow remember the y variable for each instance of the note, until a new note appears with 145 and changes it ...

clean like dirt?

+3
2

. . :

global y      -- declare y
set y as 0    -- initialize y

on function ()
    set y as (y + 1)
end function

function()    -- call function

return y

1, y . y .

: http://developer.apple.com/library/mac/#documentation/applescript/conceptual/applescriptlangguide/conceptual/ASLR_variables.html#//apple_ref/doc/uid/TP40000983-CH223-SW10

+7

? " ", 145 , , "" , 145. , ?

global detectedKey
set detectedKey to false
global modifier
set modifier to "1"
global message

set messageList to {"144,4,127", "145,5,127", "144,1,127", "144,2,127", "145,4,127", "144,1,127", "144,2,127"}


repeat with incomingMessage in messageList
    display dialog " incoming: " & incomingMessage & "\n outgoing :" & process(incomingMessage) & "\n modifier: " & modifier
end repeat

on process(incomingMessage)
    set a to item 1 of seperate(incomingMessage)
    set b to item 2 of seperate(incomingMessage)
    set c to item 3 of seperate(incomingMessage)

    if detectedKey is true then
        set outgoingMessage to "144" & "," & b * modifier & "," & c
        if a is equal to "145" then
            set detectedKey to false
                            set modifier to "1"
            set outgoingMessage to "144" & "," & b * modifier & "," & c
        end if
    else if detectedKey is false then

        if a is equal to "145" then
            set detectedKey to true
            set modifier to b
            set outgoingMessage to "144" & "," & b * modifier & "," & c
        else if a is equal to "144" then
            set outgoingMessage to a & "," & b & "," & c
        end if

    end if

    return outgoingMessage
end process



on seperate(message)
    set oldDelimiters to text item delimiters
    set AppleScript text item delimiters to {","}
    return text items of message
    set AppleScript text item delimiters to oldDelimiters

end seperate
0
source

Source: https://habr.com/ru/post/1792452/


All Articles