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

Joe*_*oel 3 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
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

像泥一样清澈?

Fáb*_*rez 9

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

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

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

function()    -- call function

return y
Run Code Online (Sandbox Code Playgroud)

这将返回,1因为您可以访问y函数内部.函数结束后,y将保留值.

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