MessageDialog code stops plugin from loading

• Mar 18, 2025 - 02:27

I am using Windows 8.1 (6.3), Arch.: x86_64, MuseScore version (64-bit): 3.6.2.548021803, revision: 3224f34

This program uses Qt version 5.9.9.

I am trying to output some data after my plugin runs.
But as soon as I add the following code, the plugin no longer reloads.
What am I missing?

var debugDialog = MessageDialog {
title: qsTr("NOBREAK Plugin Results")
text: qsTr(messageText)
standardButtons: StandardButton.Ok
onAccepted: Qt.quit // Exit after the user clicks OK.
}
debugDialog.open()

If MessageDialog is not supported, is there another way to output information on the screen?

Here is the rest of the code:

/*
NOBREAK Plugin for MuseScore
Plugin Name: NOBREAK
Description: Searches measures for a staff text equal to "_nb".
If found, the text is hidden, the measure is marked as non-breakable (NOBREAK),
and a MessageDialog lists all affected measure numbers.
Author: [Your Name]
Version: 1.0
*/

import QtQuick 2.0
import QtQuick.Dialogs 1.2
import MuseScore 3.0

MuseScore {
// Marker text to search for
property string markerText: "_nb"

// The name displayed in the plugin menu.
menuPath: "Plugins.NOBREAK"

// Entry point of the plugin.
onRun: {
    if (!curScore) {
        console.log("No score is open.");
        Qt.quit();
        return;
    }

    // Array to store measure numbers with the marker.
    var measuresWithMarker = [];

    // Create a cursor and rewind to start processing by measures.
    var cursor = curScore.newCursor();
    cursor.rewind(Cursor.REWIND_MEASURE);

    // Loop through each measure.
    while (!cursor.eos()) {
        var measure = cursor.measure;
        var foundMarkerInMeasure = false;

        // Iterate over all elements in the measure.
        for (var i = 0; i < measure.elements.length; ++i) {
            var el = measure.elements[i];
            // Check if the element is a StaffText and its text exactly matches the marker.
            if (el && el.type === ElementType.StaffText && el.text === markerText) {
                el.visible = false;   // Hide the marker text.
                foundMarkerInMeasure = true;
            }
        }

        // If we found the marker in the current measure, mark it as NOBREAK.
        if (foundMarkerInMeasure) {
            // Assuming MuseScore supports 'nobreak' property on measures.
            measure.nobreak = true;

            // Record the measure number.
            // (If your MuseScore version uses a different property, adjust accordingly.)
            measuresWithMarker.push(measure.no);
        }

        cursor.nextMeasure();
    }

    // Prepare the message text.
    var messageText = (measuresWithMarker.length > 0) ?
        "Measures with '_nb' marker found:\n" + measuresWithMarker.join(", ") :
        "No measures found";

    // Create a MessageDialog to display the results.
    /*
    var debugDialog = MessageDialog {
        title: qsTr("NOBREAK Plugin Results")
        text: qsTr(messageText)
        standardButtons: StandardButton.Ok
        onAccepted: Qt.quit   // Exit after the user clicks OK.
    }
    debugDialog.open()
    */
}

}


Comments

In reply to by a1s2d3f4

I confirm that I can see console.log messages when I use the Plugin Creator GUI in MuseScore 3 instead of my IDE to code.
But when I try to do the same in MuseScore 4, I don't see Plugin Creator there.
Do you know how I can output messages to console (or whatever it would be) when I test under MuseScore 4?

In reply to by Jojo-Schmitz

Oh, please do let me know where the log files are! I can load it into my Notepad++ and have it refresh every time the file changes. I guess I still have to go back to my manual "Reload" of the script code to test the plugin. Also, in case you know, is there a way to insert a "NO-BREAK" layout object into a measure in Mu4? Because after trying everything, I gave up in Mu3.

I can't quite tell exactly what's wrong with your code (should it be Qt.quit()? Is MessageDialog supported?), but feel free to adapt the code below which works well for me:

StyledDialogView {
    id: dialog
    title: "CHECK COMPLETED"
    contentHeight: 232
    contentWidth: 456
    property var msg: ""
    Text {
        id: theText
        width: parent.width-40
        x: 20
        y: 20
        text: "MN CHECK LAYOUT AND INSTRUMENTATION"
        font.bold: true
        font.pointSize: 18
    }   
    Rectangle {
        x:20
        width: parent.width-40
        y:45
        height: 1
        color: "black"
    }
    ScrollView {
        id: view
        x: 20
        y: 60
        height: parent.height-100
        width: parent.width-40
        leftInset: 0
        leftPadding: 0
        ScrollBar.vertical.policy: ScrollBar.AsNeeded
        TextArea {
            height: parent.height
            textFormat: Text.RichText
            text: dialog.msg
            wrapMode: TextEdit.Wrap
            leftInset: 0
            leftPadding: 0
            readOnly: true
        }
    }
    ButtonBox {
        anchors {
            horizontalCenter: parent.horizontalCenter
            bottom: parent.bottom
            margins: 10
        }
        buttons: [ ButtonBoxModel.Ok ]
        navigationPanel.section: dialog.navigationSection
        onStandardButtonClicked: function(buttonId) {
            if (buttonId === ButtonBoxModel.Ok) {
                dialog.close()
            }
        }
    }
}

it's a bit difficult to fix this code, there are too many points with improper use of the qml language and Musescore conventions. It should be corrected by examining other plugins to learn, correcting from time to time any error shown by the console.
cursor.eos()
is very nice, but unfortunately it doesn't exist... except in Greek mythology

In reply to by a1s2d3f4

Correct procedure. Beware that the 'Plugin Creator' does not notify you that a property does not exist: when something is wrong you will have to create a message like this:
console.log(cursor.eos);
(eos without the brackets, to transform it into a property) to see (in the 'Plugin Creator' window) if a certain value returns or 'undefined'. In this case you will see 'undefined'.

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