Skip to main content
Topic: Insert entity at current cursor position working example (Read 17316 times) previous topic - next topic

Insert entity at current cursor position working example

If it is a relatively straightforward task, is it possible for someone to provide a working example of a script that:
1) Displays a dialog box, in which a input textbox has focus
2) The user enters the entity in nwctxt e.g. "|Instrument|Patch:40+Enter"
3) On pressing Enter the dialog box closes, and the instrument change to patch 40 entity is then inserted at current cursor position.
Many Thanks

Re: Insert entity at current cursor position working example

Reply #1
This isn't a user tool, but it should work.  Save your file if you have made any changes to it before using this method:

  • Open Notepad at the same time as you have NWC open.
  • Select and cut (Cntl+X) the measure you need to insert something.
  • Alt+Tab to get to Notepad.
  • Paste (Alt+V) into Notepad.
  • Insert your entity after the first line.  Do Cntl+A followed by Cntl+X.
  • Alt+Tab to get to NWC.
  • Cntl+V to insert it back into your NWC file.
Since 1998

Re: Insert entity at current cursor position working example

Reply #2
Thank you so much for the above. It really is so handy. By building a library of lines for copying and pasting from Notepad into NWC, I can finally quickly insert instrument changes, decorated bar lines, dynamics etc without having to navigate through all the selection lists. Best Wishes

Re: Insert entity at current cursor position working example

Reply #3
I was wondering what you were trying to do...

Don't you think it might be easier to just learn the keyboard shortcuts?

E.G. - <Tab> or <*> inserts a barline - if you need a decorated one then:
<tab>, <Shift+LeftArrow>, <Ctrl+Shift+Up/DownArrow> (to cycle through the barline types until you get the one you want).

<i> opens the instrument change dialogue

Dynamics should be on the standard tool bars (easiest way)

Note values:
1=whole, 2=half, 3=quarter, 4=eighth, 5=sixteenth, 6=thirtysecond
Accidentals:
7=natural, 8=flat, 9=sharp

Plus lots of others.
I plays 'Bones, crumpets, coronets, floosgals, youfonymums 'n tubies.

Re: Insert entity at current cursor position working example

Reply #4
This should do it. Hardest part is converting a File header into a Clip header:
Code: (php) [Select · Download]
<?php /* simple insert demo
Ref: https://forum.noteworthycomposer.com/?topic=8312.0
Command line option:
<PROMPT:Enter a line of clip text:=*>
Recommended NWC Options:
[X] File Text
*/
require_once('lib/nwc2clips.inc');
$startfiletxt = array(NWC2_STARTFILETXT, ')'); // search
$startclip = array(NWC2_STARTCLIP, ',Single)'); // replace
foreach (gzfile('php://stdin') as $ln) {
$ln = str_replace($startfiletxt, $startclip, $ln, $count);
if ($count == 2) break;
}
$clip = new NWC2Clip(array($ln, NWC2_ENDCLIP));

echo $clip->GetClipHeader(), PHP_EOL;
if ($argc) echo $argv[1], PHP_EOL;
echo $clip->GetClipFooter(), PHP_EOL;
?>
Registered user since 1996

Re: Insert entity at current cursor position working example

Reply #5
Thanks Lawrie for your suggestion. Warren's suggestion has mostly solved what I wanted to do. I was trying to write music that had alot of dynamic changes in order to produce a realistic performance sound, e.g. multiple dynamic changes using hidden dynamic entities and volume multi-point controllers within the same measure. The keyboard shortcuts are really handy, and I use them all the time, but it can become a chore having to press for example "D+m+m+shift+V+100+Enter" just to insert an mp dynamic entity with volume 100. One has to pay attention to the content of the current selection list and values in the text boxes as well, as these values keep changing in order to preserve the last entered values. I wanted just to press Ctrl+Alt+F8+Dmpv100+Enter to do the same. In terms of number of keystrokes the saving probably isn't very great, but it means I don't have to think about what is happening inside the dialog boxes.

Thanks Rick for the code. I'll give that a go.

Best Wishes

 

Re: Insert entity at current cursor position working example

Reply #6
If you modify dynamics like that often, you might want to try this "tool": dynamics.htm.  It is used to modify dynamics where an effort must be made throughout the note such as a moving bow or supply of air. That effort, when changed while one note is playing, can change its dynamic level and this page can be used to allow crescendos and decrescendos to take place the same way.

Have it open at the same time as NWC.  Cut a staff to the clipboard, Alt+Tab to get to the webpage, Cntl+V to the textarea, change volume levels (if needed), Submit, Cntl+X, Alt+Tab, Cntl+V.
Since 1998

Re: Insert entity at current cursor position working example

Reply #7
Thank you very much. I tried the dynamics.htm out on a line consisting of a flute and violin, and it really does make such a difference in producing a humanized sound, particularly on the violin. By the way, your site is full of useful pages related to NWC. Maybe someone should start a main topic in the NWC forum entitled "useful tools, scripts and websites" so that people can get access to it, and also where I can eventually post my library of shortcut command mappings to nwctxt. Best Wishes

Re: Insert entity at current cursor position working example

Reply #8
<snip> By the way, your site is full of useful pages related to NWC. Maybe someone should start a main topic in the NWC forum entitled "useful tools, scripts and websites" so that people can get access to it, and also where I can eventually post my library of shortcut command mappings to nwctxt.

Have you seen the Noteworthy Scriptorium?  http://nwc-scriptorium.org/

There is a section devoted to user tools.

It might be time for Warren's web site to be referenced here too.  How about it Warren?  Is your site ready for you to submit it to Rich as an additional resource?
I plays 'Bones, crumpets, coronets, floosgals, youfonymums 'n tubies.

Re: Insert entity at current cursor position working example

Reply #9
It might be time for Warren's web site to be referenced here too.  How about it Warren?  Is your site ready for you to submit it to Rich as an additional resource?

It already is.  Under "Helpful Files", my site is the first thing listed under "NWC Files": Various Utilities.

By the way, your site is full of useful pages related to NWC.

Thanks.  I appreciate that, but I owe a lot to people like Rick G. on this forum.
Since 1998

Re: Insert entity at current cursor position working example

Reply #10
It already is.  Under "Helpful Files", my site is the first thing listed under "NWC Files": Various Utilities.
Oops, I clearly checked first didn't I :/
I plays 'Bones, crumpets, coronets, floosgals, youfonymums 'n tubies.

Re: Insert entity at current cursor position working example

Reply #11
I have started a page at http://geocities.ws/aardvarksong1/qwikcopypaste.html which generates nwctxt from a shortcut code entered by the user for copying and pasting into NWC. At the moment it only allows the user to insert clef, text, instrument change, volume and velocity, and MPC volume entities, but hopefully I'll be able to work on it.  It is only an HTML page with javascript code inside. Getting Started instructions are on the page. Best Wishes

Re: Insert entity at current cursor position working example

Reply #12
I tried, but couldn't get it to create anything.  After entering something like "[text test]", what makes it create something like:
Code: (nwc) [Select · Download]
|Text|Text:"text test"|Font:StaffItalic|Pos:3|Wide:N|Justify:Left|Placement:BestFit|Color:0|Visibility:Default
?

Welcome to the NWC javaScript club!  (We just doubled in size <g>.)
Since 1998

Re: Insert entity at current cursor position working example

Reply #13
Hey Warren,
are you aware the Microsoft's WScript will run .js files?

What this means is that you don't actually need a web server to run your scripts, but that WScript can run them from within NWC.  Allsame VBscript...

Just set 'em up as you would any other user tool.
I plays 'Bones, crumpets, coronets, floosgals, youfonymums 'n tubies.


Re: Insert entity at current cursor position working example

Reply #15
Hi Warren, "~s" is the command to translate to nwctxt, so typing "[test-text]~s" in the upper pane will produce "|Text|Text:"test-text"|Font:StaffItalic|Pos:-10" in the lower pane.  It's still really buggy (e.g. it can't handle spaces inside text), but hopefully it will get ironed out the more I use it.

Thanks for the Tidy.js, Rick. I tried to use it but I'm getting "Command process failed". I tried to run just a single line like "var io = WScript.StdIn.ReadAll().split("\r\n");" and this is also giving the same error on both Windows XP and Vista.  But this has given me a few new ideas to try out inside PHP, e.g. typing in the shortcut command as text, and using PHP to globally transform the text into an entity.

Best Wishes

Re: Insert entity at current cursor position working example

Reply #16
After I typed "[test-text]~s" it still did nothing.  Does this require IE?  Enter key? tab?

P.S. Just tested it with IE and it appears to work.  Doesn't work with Google Crome or Mozilla Firefox though.
Since 1998

Re: Insert entity at current cursor position working example

Reply #17
Thanks for the Tidy.js, Rick. I tried to use it but I'm getting "Command process failed".
That is an indication that there is a problem with the contents of the Command: box in the 'User Tool Description'.

The command for tools written in PHP usually start with: php\php.exe
For VBScript or JScript, it can be: WScript
or: CScript //nologo
Registered user since 1996

Re: Insert entity at current cursor position working example

Reply #18
Are you aware the Microsoft's WScript will run .js files?

What this means is that you don't actually need a web server to run your scripts, but that WScript can run them from within NWC.  Allsame VBscript...

Just set 'em up as you would any other user tool.
I guess the logic would stay the same except IO and prompts (if any) would change.  How could I get a simple page like graceacc.htm to run as a script?

TIA
Since 1998

Re: Insert entity at current cursor position working example

Reply #19
How could I get a simple page like graceacc.htm to run as a script?
Try the attachment. Test it on:
Code: (nwc) [Select · Download]
!NoteWorthyComposerClip(2.5,Single)
|Note|Dur:4th,Grace|Pos:-6^|Visibility:Never
|Note|Dur:4th,Grace|Pos:b-4^|Visibility:Never
|Note|Dur:4th,Grace|Pos:-2^|Visibility:Never
|Chord|Dur:Whole|Pos:-6,-4,-2,1
!NoteWorthyComposerClip-End
Registered user since 1996

Re: Insert entity at current cursor position working example

Reply #20
Hi Warren, apologies about the "~s" not working. I've now put "(tested on IE 9)" at the start of the page so that Chrome and Firefox users are aware.

It looks like I'm beginning to get the hang of PHP scripting for User Tools. I've created a page at http://geocities.ws/aardvarksong1/introusrtools.html for newbies like myself. After changing php.exe to WScript in the Command box, I can also run JavaScript in User Tools. I'll look more carefully next time.
I've attached a zipped up qwiktype.php to this post. To insert a treble clef, the user needs to insert a text entity with the text value '\&t', and then run the script.  Similarly '\v120' inserts a volume entity with value 120, '\c120' inserts a velocity entity with value 120, '\ivln' inserts a violin instrument change, and '\mv0,10/1,20/1,40/1,50' inserts an MPC volume increase for every 16th note duration. It's still really buggy, and its functionality is limited, but hopefully I'll add to it as I use it. Really appreciate all the help I've received in this thread. Best Wishes

(Attachment qwiktype.zip has been removed because it has been replaced with qwiktype.v.1.0.beta.1.zip later in this thread)

Re: Insert entity at current cursor position working example

Reply #21
How could I get a simple page like graceacc.htm to run as a script?
Try the attachment. Test it ...
After I parked it with the other scripts, I modified the end of nwc2UserTools.ini to read:
Code: [Select · Download]
[Developer Tools]
About Starter Kit (nwsw)=2,php\php.exe scripts\nwsw_About.php
nwctxt Clip Explorer (nwsw)=18,php\php.exe scripts\nwsw_nwctxtFileExplorer.wxphp
nwctxt File Explorer (nwsw)=31,php\php.exe scripts\nwsw_nwctxtFileExplorer.wxphp
Variable Dump for Developers (nwsw)=2,php\php.exe scripts\nwsw_VarDump.php
PHP Class Explorer (nwsw)=18,php\php.exe scripts\nwsw_phpClassExplorer.wxphp
Simple PHP File Editor (nwsw)=18,php\php.exe scripts\wxtools\nwsw_SimpleEditor.wxphp
Encode File (nwsw)=18,php\php.exe scripts\wxtools\nwsw_EncodeFile.wxphp
Test WBP=18,wscript scripts\rg_graceacc.js
but the E did not change to E flat in your example.  At this point I don't know if the script ever ran or if there was a bug in it.

Two questions:
  • What is the number just to the right of the =sign? Clip Explorer and File Explorer are identical except for this.
  • When running scripts, is there a way to display things immediately (like alert("error");)?

Many thanks for your help.
Since 1998

Re: Insert entity at current cursor position working example

Reply #22
I modified the end of nwc2UserTools.ini to
I can't imagine why. Just use NWC's Alt+F8 interface.

  • What is the number just to the right of the =sign?
It is the bit encoded representation of the 'Input Type' and 'Options' from the User Tool Description.
These are documented in nwc2clips.inc
Quote from: nwc2clips.inc
// Cmd flags set in the User Tool command line. Defiend[sic] here mostly for documentation purposes.
define("UTOOL_SEND_CLIPTXT",0);    // Selected notation clip text sent in STDIN (the default)
define("UTOOL_SEND_FILETXT",1);    // File nwctxt sent in STDIN
define("UTOOL_ACCEPTS_GZIP",2);    // The nwctxt/clip text in STDIN can be (optionally) gzip deflated
define("UTOOL_RETURNS_FILETXT",4); // Script sends a nwctxt file to STDOUT that replaces current file
define("UTOOL_LONG_TASK",8);       // Task may require a long time to finish
define("UTOOL_USES_PROMPTS",16);   // Prompts the user for input from within the script
I don't use this much, but in your case:
Test WBP=18,wscript scripts\rg_graceacc.js
it explains why your tool fails. You are telling NWC to 'Compress Input' and JScript has no way to uncompress it (without a gzip ActiveX library).

  • When running scripts, is there a way to display things immediately (like alert("error");)?
Code: (js) [Select · Download]
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.Popup("error");
You should have a file, script5?.chm somewhere. It will explain the full syntax. My help file is: c:\windows\help\script56.chm
Registered user since 1996

Re: Insert entity at current cursor position working example

Reply #23
Suppose I've got 3 notes in a string inside qwiktype.php, e.g.
Code: [Select · Download]
$threenotes="Clef|Type:Treble\n". "|Note|Dur:4th|Pos:1\n" .  "|Note|Dur:4th|Pos:1\n"  . "|Note|Dur:4th|Pos:1\n" ;
Is it possible to send them to the playback device? e.g.
Code: [Select · Download]
$MidiPlayer->playnwctxt($threenotes);
Thanks


Re: Insert entity at current cursor position working example

Reply #24
Is it possible to send them to the playback device? e.g.
Code: [Select · Download]
$MidiPlayer->playnwctxt($threenotes);
If you are asking if there is a class library with a method to 'playnwctxt', the answer is no. It should be possible to write such a class, but it would take several hours, perhaps longer if you wanted it to return to NWC while playing and clean up after itself.
Registered user since 1996

Re: Insert entity at current cursor position working example

Reply #25
I can't imagine why. Just use NWC's Alt+F8 interface.
It is the bit encoded representation of the 'Input Type' and 'Options' from the User Tool Description.
These are documented in nwc2clips.incI don't use this much, but in your case:it explains why your tool fails. You are telling NWC to 'Compress Input' and JScript has no way to uncompress it (without a gzip ActiveX library).
Code: (js) [Select · Download]
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.Popup("error");
You should have a file, script5?.chm somewhere. It will explain the full syntax. My help file is: c:\windows\help\script56.chm

Took out the "18" and it performed as advertised!!  Once again many thanks, that opens up a whole new world in the tools section for me.  Haven't tried the popup yet, but don't anticipate any problems.
Since 1998

Re: Insert entity at current cursor position working example

Reply #26
I've attached qwiktype.v.1.0.beta.1.zip qwiktext.v10b2.zip qwiktype.10a.zip to this post. The usage guide is contained inside the PHP file header, and is also will be available at http://www.geocities.ws/aardvarksong1/qwiktypeguide.html  http://www.geocities.ws/aardvarksong1/qwiktextguide.html. The Getting Started provides an example of how to input a series of notes and volume changes. Alot still needs to be done to it, but I'll submit a copy to the Scriptorium once I have a stable version. Please refer to http://nwc-scriptorium.org/nwc2scripts_qtmi.html for further details. Best Wishes

Updated 2012/11/30: replaced beta 1 zipfile with beta 2. Also changed the name from qwiktype to qwiktext as it sounds more appropriate now that insertion commands can also be read from a file.

Updated 2012/12/02: replaced beta 2 file attachment with release version 1.0

Updated 2012/12/11: replaced release version 1.0 with version 1.0a. Now renamed back to "qwiktype"

Updated 2012/12/15: Removed attachment, and added link to Scriptorium



Re: Insert entity at current cursor position working example

Reply #27
Suppose I've got 3 notes in a string inside qwiktype.php, e.g.
Code: [Select · Download]
$threenotes="Clef|Type:Treble\n". "|Note|Dur:4th|Pos:1\n" .  "|Note|Dur:4th|Pos:1\n"  . "|Note|Dur:4th|Pos:1\n" ;
Is it possible to send them to the playback device? e.g.
Code: [Select · Download]
$MidiPlayer->playnwctxt($threenotes);
I'm not yet seeing the need for a class. Here's a function to try:
Code: (php) [Select · Download]
<?php
# rg_Play.php ver 1.0
# Ref: http://my.noteworthysoftware.com/?topic=8312.0
require_once('lib/nwc2clips.inc');
$wso = new COM('WScript.Shell');
$a = array('|Clef|Type:Alto', '|Note', '|Note|Pos:2', '|Note|Dur:Whole|Pos:3');
$clip = new NWC2Clip();
$list = array($a, $clip->Items);
$opt = -1;
while (isset($list[++$opt])) {
$control = Play($list[$opt]);
switch ($wso->Popup('Repeat?', 0, $argv[0], 35)) {
case 2: $opt = -10; // Cancel
case 6: $opt--; // Yes
}
unset($control); // stops playback and releases file
}
$stat = fstat(STDOUT); if ($stat['size']) exit(NWC2RC_REPORT);

function Play($items) {
$opt = array('bypass_shell' => true);
$cmd = array('nwc-conv', 'NWCTXT', sys_get_temp_dir().'_rg_.mid');
if (!is_file($cmd[2])) register_shutdown_function('unlink', $cmd[2]);
rewind(STDIN); // aka 'php://stdin'
preg_match('/\d+\.\d+/', current(preg_grep('/^!/', gzfile('php://stdin'))), $m);
fwrite($h = tmpfile(), NWC2_STARTFILETXT.'('.$m[0].')'.PHP_EOL); // header
fwrite($h, join(PHP_EOL, $items));
fwrite($h, NWC2_STARTFILETXT.PHP_EOL); rewind($h); // footer

proc_open(join(' ', $cmd), array($h), $x, null, null, $opt);
$wmp = new COM('WMPlayer.ocx');
$wmp->URL = $cmd[2];
return $wmp;
}
?>
Registered user since 1996

Re: Insert entity at current cursor position working example

Reply #28
Thanks Rick for the code. It works fine as a standalone program, and also when I put it at the start of the globals (main) section in qwiktype.php. But the intention is to put it just before the final "exit(NWC2RC_SUCCESS)" line of qwiktype.php so that it can playback the entities that have just been newly inserted. Unfortunately it returns the error "Format error in the clip text" in l.40 of obj_NWC2Clip.php. The error occurs in the line "$clip = new NWC2Clip();". I've tried various alternatives, such as renaming the clip object (so as not to clash with the qwiktype clip object), using the qwiktype clip object inside the "$list" array for playback (this playbacks the notes before the change, and then outputs the new notes and entities as nwctxt in the stdout dialog box instead of into the NWC2 stave) etc, but still can't get it to work. If there's a quick answer to this, or if you have any initial thoughts, any help would be greatly appreciated, otherwise I'd rather park this to one side, and focus on getting a stable version of qwiktype released. Best Wishes

Re: Insert entity at current cursor position working example

Reply #29
The error occurs in the line "$clip = new NWC2Clip();".
If you look at obj_NWC2Clip.inc you will see that in the absence of an argument, new NWC2Clip(); reads from 'php://stdin'. My function also reads from it, leaving it in an unknown state.

NWC2Clip starts somewhere after the start and reaches the end without finding a header causing this:
"Format error in the clip text" in l.40 of obj_NWC2Clip.php.
The fix is to add: rewind(STDIN); before: $clip = new NWC2Clip();

STDIN and 'php://stdin' are linked such that rewinding STDIN changes the references in 'php://stdin'

Hope this helps
Registered user since 1996