我可以用这个applescript设置一个全局变量吗?

我可以用这个applescript设置一个全局变量吗?,applescript,Applescript,我对Applescript完全是个新手。我正在接收MIDI音符消息(消息)。它们的形式是三个数字(即:145、0、127) 我需要做的是听一个以145开头的midi音符编号,然后看它的“项目2”。然后我需要将其乘以8,并将其保存为midi音符编号的第2项,以144开头 每一个以145开头的音符都会有几个以144开头的音符。所以我需要保留这个变量,直到出现145个音符 问题是,我认为每当midi音符经过这个脚本时,它就会重新运行?我需要记住每个音符实例的y变量,直到出现一个带145的新音符并改变它

我对Applescript完全是个新手。我正在接收MIDI音符消息(消息)。它们的形式是三个数字(即:145、0、127)

我需要做的是听一个以145开头的midi音符编号,然后看它的“项目2”。然后我需要将其乘以8,并将其保存为midi音符编号的第2项,以144开头

每一个以145开头的音符都会有几个以144开头的音符。所以我需要保留这个变量,直到出现145个音符

问题是,我认为每当midi音符经过这个脚本时,它就会重新运行?我需要记住每个音符实例的y变量,直到出现一个带145的新音符并改变它


清除为mud?

在函数范围之外声明一个全局变量。请参见下面的示例:

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
这将返回
1
,因为您可以在函数内部访问
y
。函数结束后,
y
的值将被保留


阅读更多信息:

这个怎么样?这将通过“消息列表”,一旦数字145出现,它将作为一个打开开关,使用“修改器”修改第二个数字,直到145再次出现。这就是你想要的吗

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

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

function()    -- call function

return y

我不确定我是否理解你的问题,但你的意思是,给定三个数字(a、b、c),你想运行一个触发器,一旦a=145的数字被播放,下面的所有音符都被修改为(a)≠直到下一个a=145的音符出现?如果是,是否希望下一个触发器事件创建注释(a)≠145,b*8,c)或(a)≠145,b*8*8,c)或(a)≠145,b,c)或完全不同的东西?应该发生的是脚本正在侦听以145开头的数字。发生这种情况时,它会查看该数字的第二个值。然后,以下以144开头的每个注释都将使用该第二个值并修改它自己的第二个值(y=x*8)。这将发生,直到下一个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's text item delimiters to {","}
    return text items of message
    set AppleScript's text item delimiters to oldDelimiters

end seperate