FileIO in MuseScore 3.0.5 plugin ?

• Mar 14, 2019 - 14:06

Hello

FileIo - to write text file - is not available in MuseScore 3 ?

In MuseScore-2 it was OK ( code sample hereafter)
In MuseScoe 3.0.5 it makes "module "FileIO" version 1.0 is not installed"
Thank you for your help

// Version MuseScore 3 NOT OK //
////////////////////////////////////

import QtQuick 2.2
import MuseScore 3.0
import FileIO 1.0
MuseScore {
      version:  "3.0"
      description: qsTr("test plugins musescore 3")
      menuPath: "Plugins.Notes.test"
      FileIO { // generates module "FileIO" version 1.0 is not installed
            id: outfile
            source: "pos.txt"
            onError: console.log(msg)
      }

// Version MuseScore 2 OK //
////////////////////////////////////

import QtQuick 2.0
import MuseScore 1.0 
import FileIO 1.0
 
MuseScore {
      version:  "1.0"
      description: "plugin scan position"
      menuPath: "Plugins.Notes.scan_position"
 
      // output file to save the positions of the notes
      FileIO {
            id: outfile
            source: "pos.txt" 
            onError: console.log(msg)
      }

Comments

In reply to by Frank Revolle

Example of code to write tick/pitches in a text file, for MuseScore3.
If it can help other users...

import QtQuick 2.2
import MuseScore 3.0
import FileIO 3.0
MuseScore {
  version:  "3.0"
  description: qsTr("test plugins musescore 3")
  menuPath: "Plugins.Notes.test"

  // output file to save the notes
  FileIO {
        id: outfile
        source: "/tmp/test.txt" // to be changed according your configuration
        onError: console.log(msg)
  }

  function listnotes() {
        var cursor = curScore.newCursor();
        cursor.rewind(1);
        var startStaff = 0;
        var endStaff = curScore.nstaves - 1;
        var mtext = ""
        for (var staff = startStaff; staff <= endStaff; staff++) {
              console.log("  listnotes staff=" + staff);
              for (var voice = 0; voice < 4; voice++) {
                    console.log("    listnotes voice=" + voice);
                    cursor.rewind(1); // sets voice to 0
                    cursor.voice = voice; //voice has to be set after goTo
                    cursor.staffIdx = staff;
                    cursor.rewind(Cursor.SCORE_START) // beginning of score
                    while (cursor.segment) {
                          var e = cursor.element;
                          if (e) {
                                console.log("type:", e.name, "at  tick:", e.tick, "color", e.color);
                                if (e.type == Element.REST) {
                                      console.log("rest!")
                                      }
                                if (e.type == Element.CHORD) {
                                      console.log("chord!")
                                      var notes = e.notes;
                                      if ( notes ) {
                                            for (var k = 0; k < notes.length; k++) {
                                                  var note = notes[k];
                                                  console.log("       listnotes note=" , k , " pitch=" , note.pitch);
                                                  mtext = mtext + "tick=" + e.tick + " pitch=" + note.pitch + "\n";
                                                  }
                                            }
                                      else {
                                            console.log("        listnotes zero note");
                                            }
                                      }
                          }
                          cursor.next();
                    }
              }
        }
        outfile.write(mtext);
  }

  onRun: {
        console.log("hello test plugins musescore 3");
        if (typeof curScore === 'undefined')
              Qt.quit();
        listnotes()
        Qt.quit();
     }
}

In reply to by Frank Revolle

I'm digging this one. Thanks for sharing your code.

Is there a way to retrieve the path where the plugins are installed ?

source: "./myplugin.properties"/ is in MuseScore bin folder and is not suitable for storing a plugin's property file.
source: Qt.resolvedUrl("myplugin.properties") leads to a non writeable file.
source: homePath() + "/myplugin.properties" is in the middle of nowhere, user's root folder. Not even the "MyDocument" folder. This is the best I've got for now.
rootPath(), currentPath(), tempPath() are all returning errors ("ReferenceError: rootPath is not defined")

In reply to by jeetee

Thanks. It's working fine. Although as my goal is to save a library of special fingerings (for my Alternative fingering plugin), I wonder if saving this in the registry is a good idea, because you can't backup it, and because of the size of what I would have to store.
I would prefer a regular file.

In case anyone needs to use a file stored in Musescore's plugin folder.
In this case, it manages a file in a fictive "mypluginfiles" subfolder.

readonly property var librarypath: { {
        var f = Qt.resolvedUrl("mypluginfiles/myplugin.library");
        f = f.slice(8); // THIS IS THE TRICK  : remove the "file:///" added by Qt.resolveUrl and not understood by the FileIO API
        return f;
    }
}


FileIO {
    id: libraryFile
    source: librarypath
    onError: {
        console.log(msg);
    }
}

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