At the moment, I'm learning the Lua script language. I want to experiment with writing some simple user tools, but I don't know how to access stdin, stdout and stderr.
Access to stdin is by nwcut.items, stdout is by nwcut.writeline, and stderr is by nwcut.warn.
Here is a quick example that will flip a RestChord:
nwcut.setlevel(2)
for item in nwcut.items() do
if item:Is("RestChord") then
item.Opts.Opts.Stem = item.Opts.Opts.Stem == "Up" and "Down" or "Up"
end
nwcut.writeline(item)
end
if #arg > 0 then -- any arg sets warning mode
nwcut.warn(nwcut.clock(), " seconds")
end
More info at: http://lua.noteworthycomposer.com/
Many thanks, Rick!
A few other questions :
- What does setlevel() do?
- How do I access the arguments?
- Is there any documentation on nwcut available?
See: http://lua.noteworthycomposer.com/nwcut.html##(nwcut).setlevel
They are in a global table:
argSee: http://lua.noteworthycomposer.com/nwcut.html
Thanks again, Rick.
Now I have something to read, learn and experiment...
My first problems:
1) I want to write several lines on stderr, so I included a string "\r\n" (return/newline) but that has no effect
See line 4 in:
nwcut.setlevel(2)
local DynamicFound = false
for i = 1, #arg do
nwcut.warn(arg[i], "\r\n")
end -- for i
for item in nwcut.items() do
if item:Is("Dynamic") then
if arg[1] == "Absolute" then
item.Opts.Pos = arg[2]
elseif arg[1] == 'Relative' then
item.Opts.Pos = item.Opts.Pos + arg[2]
elseif arg[1] == 'Keep_position_first_dynamic' then
local FirstPos = DynamicFound and FirstPos or item.Opts.Pos
item.Opts.Pos = FirstPos
end --if arg
end -- if item
nwcut.writeline(item)
end --for item
This is what I get in stderr:
This is what I expected:
2)
How can I use the return codes as explained in http://www.noteworthysoftware.com/nwc2/help/intro_usertooldev.htm (http://www.noteworthysoftware.com/nwc2/help/intro_usertooldev.htm)
I want to use return code 1 in case of an error and 0 for a warning. Reason : with nwcut.warn, the user can click 'OK' or 'Cancel'. I don't want that in case of an error.I found out : http://lua.noteworthycomposer.com/nwcut.html##(nwcut.const).rc_Error (http://lua.noteworthycomposer.com/nwcut.html##(nwcut.const).rc_Error)
Don't use "\r". Just use "\n".
Two other problems:
1)
local NoteLengths = { "16th" = 48, "32th" = 24, "4th" = 192, "64th" = 12, "8th" = 96, "Half" = 384, "Whole" = 768 }
gives this error:
'}' expected near '='
2)
local function ShowVar(name, var)
if Testing then
nwcut.warn(name.." ==> ", var, "\n")
end -- if
end -- ShowVar
local line = item:__tostring()
ShowVar("line", line)
local durs = item.Opts.Dur
ShowVar("#durs",#durs)
for i = 1 , #durs do
ShowVar("dur"..i , durs[i])
end -- for
I expected 'durs' to be a string = "Half", but when I treated it as a string I got an error stating that it was a table. So I included the ShowVar() calls, with following result:
What did I do wrong?
That code looks correct to me too, but you might want to look at the preceding or following statement. Sometimes Lua will "complain" about the wrong line.I think you need to put [ ] around the keys in your assignment (i.e. around the "16th", etc.). Also, you probably want to change 32th to 32nd :)
Mind you, I have not done much with nwcut, but I think your problem is that Dur is not part of the Opts segment of the nwctext. (Opts only contains the Stem=Down part.) Try changing "item.Opts.Dur" to "item.Dur".
Mike
The square brackets do the job! I also changes 32th into 32nd, thanks.
That's not the reason. For accessing the Stem, I have to use "item.Opts.Opts.Stem".
Thanks anyway, I'm sure Rick will come with the answer soon :)
setlevel(1) is a string. setlevel(2) is a table mostly of keys, i.e. a Set. The exception is Triplet which might be: 'First', '', or 'End'
!NoteWorthyComposerClip(2.0,Single)
|Note|Dur:8th,Triplet=First|Pos:0|Opts:Stem=Down
|Note|Dur:8th,Triplet|Pos:0|Opts:Stem=Down
|Note|Dur:8th,Triplet=End|Pos:0|Opts:Stem=Down
!NoteWorthyComposerClip-End
I realise now I didn't show the output of my coding. Here are both the code and the output
local function ShowVar(name, var)
if Testing then
nwcut.warn(name.." ==> ", var, "\n")
end -- if
end -- ShowVar
local line = item:__tostring()
ShowVar("line", line)
local durs = item.Opts.Dur
ShowVar("#durs",#durs)
for i = 1 , #durs do
ShowVar("dur"..i , durs[i])
end -- for
Output:
I had setlevel(2). That's why I included to _tostring line, to show the contents of item.
The result shows that the item.Opts.Dur is nil. Why isn't it = "Half"?
Now that I've thought about it a bit more, I think the problem is #durs. I think you would be better off iterating through the table using pairs(..) rather than a counting loop.
This article describes the issue a bit better:
http://stackoverflow.com/questions/2705793/how-to-get-number-of-entries-in-a-lua-table
Mike
Thanks Mike, pairs() does it!