2011-02-16 154 views
3
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)我可以用这个applescript设置一个全局变量吗?

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

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

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

清晰如泥?

+0

我不知道我理解你的问题的权利,但你的意思是给定三个数(A,B,C)要运行,一旦被= 145多家被起了扳机,所有以下注释被修改为(a≠145,b * 8,c),直到a = 145的下一个音符出现为止?如果是这样,是否希望下一个触发事件创建音符(a≠145,b * 8,c)或(a≠145,b * 8 * 8,c)或(a≠145,b,c)或完全不同的东西? – Asmus 2011-02-16 09:36:29

回答

7

在函数范围外声明一个全局变量。看下面的例子:

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

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 
相关问题