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'm a complete newbie to Applescript. I am receiving MIDI note messages (message). They take the form of three numbers (IE: 145, 0, 127)
What I need to do is listen for a midi note number starting with 145, and then look at its' item 2. I then need to multiply that by 8 and save that as the item 2 of a midi note number starting with 144.
There will be several notes starting with 144 for every note with 145. So I need to keep that variable until a 145 note comes along.
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 every instance of note until a new note with 145 comes along and changes it...
clear as mud?
Declare a global variable outside the function scope. See the example below:
global y -- declare y
set y as 0 -- initialize y
on function ()
set y as (y + 1)
end function
function() -- call function
return y
This will return 1
since you can access y
inside of the function. After the end of the function, the value of y
will be preserved.
Read more: http://developer.apple.com/library/mac/#documentation/applescript/conceptual/applescriptlangguide/conceptual/ASLR_variables.html#//apple_ref/doc/uid/TP40000983-CH223-SW10
How about this? This will go through the "messageList" and once the number 145 turns up, it will work as an on toggle to modify the second number with the "modifier" until 145 turns up again. Is that what you want?
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
精彩评论