Deleting / Removing measures using a plugin

• May 23, 2021 - 01:04

How can you delete /remove measures from a score using a plugin? In particular I wish to delete the last measure in a score.

There is a 'del-empty-measures' command, but I cannot see a command to delete a particular measure. There are commands to 'insert-measure' and 'append-measure'

The 'score' object has an 'appendMeasures' method. but I cannot see an insert measures option or a delete measure method.

Barry


Comments

In reply to by jeetee

Here is sample code that will select a given measure (the code is written in the context of wanting to delete the selected measure - but trying to do the actual deletion will cause musescore to crash unless the last measure in the score is part of the selection):

         //Set number of the measure that is to be selected/deleted
        var measureNumToBeDeleted = 2

        if (measureNumToBeDeleted > curScore.nmeasures || measureNumToBeDeleted < 1) {
              console.log("Measure to be deleted is greater than the number of measures in the score (or less than 1)")
        }
        else { 

              //Initialise the 'measureToBeSeletected variable and then 
              //step through measures sequentially until the measure corresponding 
              //to the measure to be deleted is reached.
              var measureToBeSelected = curScore.firstMeasure

              for (var i = 2; i <= measureNumToBeDeleted; i++) {
                    measureToBeSelected = measureToBeSelected.nextMeasure                                    
              }

              //'measureToBeSelected' should now be the correct measure 
              //i.e. the measure that is ultimately be deleted

              var startTick = measureToBeSelected.firstSegment.tick
              var endTick   = measureToBeSelected.lastSegment.tick

              //Select the measure - first stave only
              //The use of 'endTick+1' is necessary if  the last measure in the score is being selected, otherwise it is not necessary and does not impact the selection
              //curScore.selection.selectRange(startTick, endTick+1, 0, 0)

              //Select the measure - all staves
              //The use of 'endTick+1' is necessary if  the last measure in the score is being selected, otherwise it is not necessary and does not impact the selection
              //If only 'endTick' is used the last note of the last measure in the score will not be selected (if the last measure is part of the selection)
              console.log("Number of Staves: " + curScore.nstaves)
              curScore.selection.selectRange(startTick, endTick+1, 0, curScore.nstaves)

              //Delete the selected measure
              //The next line of code will cause MuseScore to crash if the last
              //measure is not part of the selection
              //cmd("time-delete")
        }

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