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

时间:2011-02-16 08:27:38

标签: 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

我是Applescript的新手。我正在接收MIDI音符消息(消息)。它们采用三个数字的形式(IE:145,0,127)

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

对于145的每个音符,将会有144个以144开头的音符。所以我需要保留该变量,直到出现145个音符。

问题在于,我认为每次midi音符通过时,此脚本都会运行新的?我需要以某种方式记住每个音符实例的y变量,直到带有145的新音符出现并更改它...

清楚如泥?

2 个答案:

答案 0 :(得分:8)

在函数范围之外声明全局变量。请参阅以下示例:

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

答案 1 :(得分:0)

这个怎么样?这将通过“messageList”,一旦数字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's text item delimiters to {","}
    return text items of message
    set AppleScript's text item delimiters to oldDelimiters

end seperate