2
Here’s the python code. Enjoy. 
This code needs to import nwctxt.py see https://github.com/nwsw/nwc2ut-python
Love and peace,
Joe
"""joeSelReverse.py Reverses the Notes that are selected in a NoteWorthy Composer staff
see:
https://github.com/nwsw/nwc2ut-python
"""
import sys
# import joeNWCtxtInPythonNew as nwctxt
import nwctxt # see https://github.com/nwsw/nwc2ut-python
import logging
logging.basicConfig(filename='joeNWC_PY.log',level=logging.DEBUG) # DEBUG => print ALL msgs
clip = nwctxt.NWC2Clip()
PlayContext = nwctxt.NWC2PlayContext()
txtItemL = clip.Items
clipItemL = [nwctxt.NWC2ClipItem(txtItem) for txtItem in txtItemL]
first2 = clipItemL[:2]
reversedClipItemL = first2 + list(reversed(clipItemL[2:]))
reversedTextItemL = [item.ReconstructClipText() for item in reversedClipItemL]
print clip.GetClipHeader()
for textItem in reversedTextItemL[2:]:
#print "clipItem.GetObjType()= %s, type(clipItem.GetObjType())= %s, "%(clipItem.GetObjType(), type(clipItem.GetObjType()))
print textItem
print clip.GetClipFooter()
# TO DEBUG uncomment below to report to NWC STDOUT DIALOG & to CAUSE NO STAFF MODIFICATION
# sys.exit(nwctxt.NWC2RC_REPORT)
3
Dear NWC Forum,
Thanks for being a great place to share info. I am trying to write my first User Tool. It will be in Python. It is very simple. It copies a clip to a file.
I am using NWC 2.5.5 & have installed version 2.5 of the NWC2 User Tool Starter Kit.
Here's some system info from About Starter Kit:
System => Windows NT cmptrName 5.1 build 2600 (Windows XP Professional Service Pack 3) i586
Build Date => Sep 17 2009 19:13:11
Compiler => MSVC9 (Visual C++ 2008)
Architecture => x86
Configure Command => cscript /nologo configure.js "--disable-all" "--enable-cli" "--enable-zlib"
Server API => Command Line Interface
Here is my simple Python program to copy a clip to a file:
import sys
instr = sys.stdin.read()
f = open('joesFirstTestOut.txt', 'w'); f.write(instr); f.close()
When I run it from a windows prompt, it works :
C:\...Scripts>python joesFirst.py<joesFirstTestIn.txt
When I run it from a NWC User Tool, there is NO CHANGE in joesFirstTestOut.txt.
My User Tool Description Dialog has been set with 2 alternatives with the same results as above:
Alt-1
group: joe
name: joesFirst.py
command: python scripts\joesFirst.py
input type: clip text
options:
compress input: [ ]
returns file text: [ ]
long task handling: [ ]
prompts for user input: [ ]
Alt-2
group: joe
name: joesFirst.py
command: python scripts\joesFirst.py
input type: clip text
options:
compress input: [ ]
returns file text: [ ]
long task handling: [X]
prompts for user input: [ ]
Does anyone have any suggestions?
Thanks in advance.
Love and peace,
Joe
7
Here is a Python script I wrote after reading NWC Virtuoso, kahman's post:
Python NWCTXT parser - https://forum.noteworthycomposer.com/?topic=6097.0
My script converts kahman's Python List/Dictionary output to XML. I did it so that I could study the structure of an nwctxt file using a data folding text editor. The XML structure is my own home grown version based on what I inferred from kahman's Python Data Structure. I have done minimal testing so use it with caution. Please let me know what you think. Please post any additions here. You can use this script as you like. Here is the code:
"""
nwcPy2nwcXml.py Translates
FROM: nwcPythonListDictFormat (kahman's nwctxt to python converter pyNWC-a output)
TO: jdorocak's homegrown xml (So I can use data folding in a text editor.) :)
Usage: nwcPy2nwcXml.py infileName outfileName
Example: nwcPy2nwcXml.py mySong.py mySong.xml
Author: Joe Dorocak
License: Use this as you please. :)
Use with caution - I have done minimal testing. No warranty is implied. :)
"""
class ConvertToXML():
"""
Converts
FROM: nwcPythonListDictFormat (NWC Virtuoso kahman's nwctxt to python converter pyNWC-a output)
TO: jdorocak's homegrown xml (So I can use data folding in a text editor.) :)
rs == resultString; il == indentationLevel; aprs(il,str) == appendToResultString(indentationLevel,strToAppend)
data == string output of kahman's nwctxt to python converter
ix == index of current pythonListDict item;
joe's xml syntax for nwctxtPy files inferred from analysis of data (i added elements named j*):
<jNwctxtPy>
<SongInfo count=1></SongInfo>
<PgSetup count=1></PgSetup>
<jFonts><Font count=12></Font></jFonts>
<PgMargins count=1></PgMargins>
<jStaves>
<jStaff>
<AddStaff count=1></AddStaff>
<StaffProperties count=2></StaffProperties>
<StaffInstrument count=1></StaffInstrument>
<Clef count=n></Clef>
<Key count=n></Key>
<Tempo count=n></Tempo>
<Note count=n></Note>
<Chord count=n></Chord>
<Bar count=n></Bar>
</jStaff>
</jStaves>
</jNwctxtPy>
"""
def __init__(self, indata):
"""
data == data to be converted. FROM: nwcPythonListDictFormat (kahman's nwctxt to python converter pyNWC-a output)
rs == resultString; converted data jdorocak's homegrown xml (So I can use data folding in a text editor.) :)
"""
self.data = eval(indata)
self.data.append(['End', {'Joe': 'End', 'Pos': 'Whatever'}])
self.rs = ''
def toxml(self):
self.aprs(0,'<jNwctxtPy>')
#SongInfo
self.aprs(1,'<SongInfo count=1>')
ix = 0
print len(self.data)
for k,v in self.data[ix][1].iteritems():
self.aprs (2,'<' + k + '>' + str(v) + '</' + k + '>')
self.aprs (1,'</SongInfo>')
#PgSetup
self.aprs (1,'<PgSetup count=1>')
ix = 1
for k,v in self.data[ix][1].iteritems():
self.aprs (2,'<' + k + '>' + str(v) + '</' + k + '>' )
self.aprs (1,'</PgSetup>')
#Fonts
self.aprs (1,'<jFonts>')
for ix in range(2,14):
self.aprs (2,'<Font count=12>')
for k,v in self.data[ix][1].iteritems():
self.aprs (3,'<' + k + '>' + str(v) + '</' + k + '>' )
self.aprs (2,'</Font>')
self.aprs (1,'</jFonts>')
#PgMargins
self.aprs (1,'<PgSetup count=1>')
ix = 15
for k,v in self.data[ix][1].iteritems():
self.aprs (2,'<' + k + '>' + str(v) + '</' + k + '>' )
self.aprs (1,'</PgSetup>')
self.aprs (1,'<jStaves>')
self.aprs (2,'<jStaff>')
#AddStaff
ix = 16
for ix in range(ix,len(self.data)-1):
#if self.data[ix+1][0] == 'AddStaff': self.aprs (2,'<jStaff>') #ix+1 needed to add ['End',{'Joe':'whatever'}]
self.aprs (3,'<'+ self.data[ix][0] +'>')
for k,v in self.data[ix][1].iteritems():
self.aprs (4,'<' + k + '>' + str(v) + '</' + k + '>' )
self.aprs (3,'</'+ self.data[ix][0] +'>')
if self.data[ix+1][0] == 'AddStaff': #ix+1 needed to add ['End',{'Joe':'whatever'}]
self.aprs (2,'</jStaff>')
self.aprs (2,'<jStaff>')
self.aprs (2,'</jStaff>')
self.aprs (1,'</jStaves>')
self.aprs(0,'</jNwctxtPy>')
return self.rs
def aprs(self, il, strToAppend):
"""aprs(il,str) == appendToResultString(indentationLevel,strToAppend)"""
self.rs = ''.join([self.rs, il*2*' '+strToAppend+'\n'])
#print il*2*' '+strToAppend
import sys
N_ARGS = 2
def main(argv=None):
if argv == None: argv=sys.argv
args = argv[1:] #NOTE: args[0] != argv[0] == <full path to example.py>
if len(args) != N_ARGS or "-h" in args or "--help" in args:
print __doc__
sys.exit(2)
f = open(args[0], 'r'); s = f.read(); f.close() # get the input file into s
cx = ConvertToXML(s) # make a new converter for s
sXml = cx.toxml() # translate s into sXml
f = open(args[1], 'w'); f.write(sXml); f.close() # write str(s2) into the output file
print 'Translated ' + str(args[0]) + ' yielding ' + str(args[1])
if __name__ == '__main__':
sys.exit(main())
I have posted the addition I did to kahman's code to save his output in a file, at his post: https://forum.noteworthycomposer.com/?topic=6097.0
All the best. 
Joe