Python NWC tool to reverse selected notes in a staff

Here’s the python code. Enjoy.

This code needs to import nwctxt.py see https://github.com/nwsw/nwc2ut-python

"""joeSelReverse.py Reverses the Notes that are selected in a NoteWorthy Composer staff

"""

import sys
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) 

Advertisements

How 2 Create a Python NWC User Tool

Preamble

Read Introduction to User Tool Development. Summary:

  • NWC Staff Clip input to your tool is from STDIN.
  • Main Output back to NWC Staff is to STDOUT.
  • User Interaction via “PROMPT” command line arguments used when calling your tool.

Create your Python Module

Make a Python script to do something to a NoteWorthy Composer clip. Here is a simple example. It copies the NWC clip into a file.

import sys
instr = sys.stdin.read()
f = open('joesFirstTestOut.txt', 'w'); f.write(instr); f.close()

Hook Up your Python Module with NWC

1. Copy your module to:
<path>\Program Files\NoteWorthy Composer 2\Scripts\
2. Hook up NWC to your module
2.1 In NWC Click Menu >> Tools >> User Tools
2.2 In User Tools dialog click "New" button
2.3 In User Tool Description dialog enter:

Group: joe                               <or whatever>
Name: joesFirst.py                       <or whatever>
Command: python scripts\joesFirst.py     <or python scripts\whatever>
Input Type: clip text
Options: 
    compress input:         [ ]
    returns file text:      [ ]
    long task handling:     [ ]
    prompts for user input: [ ]
2.4 In User Tool Description dialog click "OK" button
2.5 In User Tools dialog click "Close" button

Run your Python Module

2.1 In NWC select the staff for which you want the clip
2.2 In NWC Click Menu >> Tools >> User Tools
2.3 In User Tools dialog select your group & command
2.4 In User Tools dialog click "Run" button

N.B. For the example module, the file named 'joesFirstTestOut.txt' 
will appear:
    NOT IN <path>\Program Files\NoteWorthy Composer 2\Scripts\
    BUT IN <path>\Program Files\NoteWorthy Composer 2\Scripts\

For More Information about NWC2 Scripting

Read also Getting started with NWC2 User Tools