How to create a text file from a Plugin

• Oct 5, 2011 - 01:47

I am writing a plug in to output details about each note in a score. I am using an existing plug in
that colours notes as my learning example.

Now I am stuck on how to open a file for writing. I included the following code, my plug in runs
with no apparent errors but no file is created.

// This code is at the beginning of the run() declaration:

var file = new QFile(pluginPath + "/test.txt");
var textStream = new QTextStream(file);

textStream.writeString("Script started " + "\n");

// I assume that all output is through this writeString method?

// at the end of the run method I close the file

file.close();

/////////////

what am I missing?

And where can I get documentation on the QFile and QTextStream methods?

Thanks
Charles


Comments

In reply to by ozcaveman

I changed the code to prompt for a file name and every message box in the code below displayed

function run() {
var cursor = new Cursor(curScore);
var fName = QFileDialog.getSaveFileName("Dialog 1", "Select .csv file to create",
"C:/temp", "CSV file (*.csv)", 0);
if(fName == null || fName == " ")
return;

var file = new QFile(fName);
var textStream = new QTextStream(file);
QMessageBox.critical("", "File Name", fName); // This displayed

textStream.writeString("Script started " + "\n");
textStream.writeString("Script started 2" + "\n");
QMessageBox.critical("", "Two Lines written", "Written"); // displayed
textStream.writeString("Script started 3 " + "\n");
file.flush();
file.close();
QMessageBox.critical("", "File Closed", "Closed Now"); // this got displayed

But no file was created!

In reply to by [DELETED] 5

The purpose of my plugin is to export a score to MUP format - a plain text language for the MUP music publishing program.

I ran my script with the Debugger enabled but I am not sure what I am looking for. I pressed the
play icon (green triangle) and the script ran. I also ran the score statistics plugin in the debugger
and it ran successfully.

I tried another approach:

I made a very simple plugin to create a text file and write hello world. I see all the
Message Box displays but no file is written.

// Supersimple plugin. prompts for a text file and writes Hello World
function init()
{
}

function run() {
var fName = QFileDialog.getSaveFileName("Dialog 1", "Select .txt file to create",
"C:/temp", "TXT file (*.txt)", 0);

if(fName == null || fName == " ")
return;

var file = new QFile(fName);

var textStream = new QTextStream(file);
QMessageBox.critical("", "File Name is ", fName);

textStream.writeString("Hello World " + "\n");
textStream.writeString("Script started 2" + "\n");
textStream.writeString("Script started 3 " + "\n");
// file.flush();
file.close();
QMessageBox.critical("", "File Closed", "Closed Now");
}

var mscorePlugin = {
menu: 'Plugins.OneLine',
init: init,
run: run
};

mscorePlugin;

In reply to by ozcaveman

I compared my script to scorestats and realised I had left out the line of code that creates the
file.

I added the following line after the line var file = new QFile(fName);

if( !file.open(QIODevice.ReadWrite) )
{ QMessageBox.critical("", "File Error", "Could not create " + fName);
return;
}
/////

I didnt realise the file needs to be created with this call. I am used to Python syntax!

Charles

In reply to by [DELETED] 5

The MusicXML project went on hold but I think I will complete it.

I realise that I can only get some score information through the plug in. I can't get the
staccato or tenuto marks, dynamics text (f, p, mf, mp ... ), phrase marks, hairpins, etc.

I will complete this plugin project which is two parts - ExportNotes which exports chord, note
and rest data to a CSV file. This script is now complete. The second part is the Notes2MUP Python
script which takes this CSV file and constructs a MUP file.

After this is operational I will release it, then move on to MusicXML2MUP.py

Thank you for your interest!

Charles

In reply to by [DELETED] 5

LIlypond - the quality of output is great but I found it too overwhelming to learn and the functionality
kept changing between released. Old version scores could not be converted easily even using the convert script. My main interest in music typesetting is to create MIDI files to drive software synthesisers - in this respect Musecore is very good.

MUP - have been using for around 5 years and I know the language very well. I often transcribe music
from printed piano music and typing MUP is quite fast. MIDI output is great and I have written many
macros for dynamics (f, p, etc........). I also have a lot of scores in MUP.

Musescore - overall I am very pleased with Musescore and I can use my MIDI keyboard to enter notes.
I am still becoming proficient with Musescore and recently I typeset a 2 page Khachaturian piece.
I like the way Musescore can import so many formats, use soundfounts for rapid prodution of playable
music, creation of MIDI and PDF.

in summary .. I am a fan of Musescore and learning to use it efficiently.

Charles

In reply to by ozcaveman

Since this thread has been resurrected and I hadn't followed it first time around, I thought I'd put in a plug for what I think is the "best" text-based music notation language in terms of human read/write-ability: ABC. It's a lot older than MUP and has a large and active community of users, with not just one typesetting / MIDI rendering program, but many different ones, along with other ABC-related utilities. The "state of the art" with regard to typesetting programs for ABC is "abcm2ps", which in my opinion can actually beat LilyPond at its own game in many respects.

No reason anyone already comfortable with MUP should change, but when I saw the list above comparing LilyPond, MUP, and MuseScore, I thought ABC should be mentioned as well for the benefit of anyone stumbling onto this thread while searching for info on text based music notation languages. MusicXML of course too, but that is obviously the subject of enough other discussion on the thread.

Because this thread contains info on MUP <> MusicXML, I'm cross posting a mail from the MusicXML mailing list coming from L Peter Deutsch:

Dear fellow MusicXML users,

I'm pleased to announce that the mup-import and MusicXML-export modules of
esconvert, a music file format conversion program that is a spinoff from my
work on score editing, have reached the 0.1 stage, so I'm soliciting mup
files for testing.

The test submission window starts today (Jan. 23) and runs through Friday
(Jan. 27). To submit your score, please package all necessary mup files in
a single archive (.zip or .tar.gz) and e-mail it to me before midnight
Friday. I will not disclose anything you send me to anyone else, will keep
it behind a firewall, and will delete it when I am done using it for
testing.

There are, of course, tons of mup files freely available on the Web (e.g.,
at cpdl.org), but I'd rather work directly with composers to whom I know
feedback will be useful.

The only reason I'm not simply putting the code of esconvert out there for
anyone to run is that it's very awkward to package, since it's written in
JavaScript with a Python wrapper. I just don't want to take the time right
now to figure out how to make it convenient to run. Maybe for 0.2. If
anyone has any suggestions for how to make this easier, please let me know.

In case you're wondering, the code will eventually be free to download,
use, and/or modify, and to redistribute non-commercially. It will probably
not be Open Source, because I probably will not want to allow commercial
redistribution. Any thoughts on that subject will be welcome.

Conversion in the opposite direction is also under development. Right now,
the mup-export module is as mature as mup-import, but the MusicXML-import
module is not as mature as MusicXML-export.

Thanks -

L Peter Deutsch

Do you still have an unanswered question? Please log in first to post your question.