TypeError: Property 'goToSelectionStart' of object Ms::Cursor(0x55d0db053b20) is not a function

• Jun 26, 2019 - 12:06

I am creating a plugin for Musescore (currently I am using version 2.3.2).

The plugin has to reverse the order of notes in triplets or other nplets of a selection.
I have not tested the algorithm because a blocking error is issued.
You can find the plugin source code in the attached file.
Some parameters are hard-coded in this preliminar version as you can see.
The error is:
TypeError: Property 'goToSelectionStart' of object Ms::Cursor(0x55d0db053b20) is not a function
I think the cursor variable is not good reference.
What is wrong?
(If other errors will be issued after the solution to this I will not open another thread but I will stick with this because I am new to MS plugin development).
Thank you

Attachment Size
example.qml 1.04 KB

Comments

What gave you the idea that such a property or method exists? The plugin edit help for cursor doesn't show it.
Check what other plugins do instead, e.g. ColorVoices:

   // Apply the given function to all chords/rests in selection
   // or, if nothing is selected, in the entire score
   function applyToChordsAndRestsInSelection(func) {
      var cursor = curScore.newCursor()
      cursor.rewind(1)
      var startStaff
      var endStaff
      var endTick
      var fullScore = false
      if (!cursor.segment) { // no selection
         fullScore = true
         startStaff = 0 // start with 1st staff
         endStaff = curScore.nstaves - 1 // and end with last
         }
      else {
         startStaff = cursor.staffIdx
         cursor.rewind(2)
         if (cursor.tick == 0) {
            // this happens when the selection includes
            // the last measure of the score.
            // rewind(2) goes behind the last segment (where
            // there's none) and sets tick=0
            endTick = curScore.lastSegment.tick + 1
            }
         else
            endTick = cursor.tick
         endStaff = cursor.staffIdx
         }
      console.log(startStaff + " - " + endStaff + " - " + endTick)
      for (var staff = startStaff; staff <= endStaff; staff++) {
         for (var voice = 0; voice < 4; voice++) {
            cursor.rewind(1) // sets voice to 0
            cursor.voice = voice //voice has to be set after goTo
            cursor.staffIdx = staff
 
            if (fullScore)
               cursor.rewind(0) // if no selection, beginning of score
 
            while (cursor.segment && (fullScore || cursor.tick < endTick)) {
               if (cursor.element) {
                  if (cursor.element.type == Element.REST)
                     func(cursor.element, voice)
                  else if (cursor.element.type == Element.CHORD) {
                     func(cursor.element, voice)
                     var graceChords = cursor.element.graceNotes;
                     for (var i = 0; i < graceChords.length; i++) {
                        // iterate through all grace chords
                        func(graceChords[i], voice)
                        var notes = graceChords[i].notes
                        for (var j = 0; j < notes.length; j++)
                           func(notes[j], voice)
                        }
                     var notes = cursor.element.notes
                     for (var i = 0; i < notes.length; i++) {
                        var note = notes[i]
                        func(note, voice)
                        }
                     } // end if CHORD
                  } // end if element
               cursor.next()
               } // end while cursor
            } // end for loop
         } // end for loop
      }

In reply to by P5music

It is, go to your link, scroll down and click on "up" to see.
But whatever, this is 1.x plugin documentation, and 2.x documentation is entirely inside the plugin edit's help, the 1.x documentation may help at times though, like to understand tonal pitch class

In reply to by Jojo-Schmitz

Ok thank you.
I changed my code for 2.x standard, you can see it as an attached file.
But It does nothing. I select the right notes (no check is performed about the element types because it is a tool for myself, I know that the selection is always right) but they are left unchanged.
What is not right? Is it the because of the algorithm, or other things?

Attachment Size
example.qml 1.1 KB

In reply to by P5music

In your code...

:
var cursor = curScore.newCursor();
cursor.rewind(2);
var lastNote=cursor; // This just makes a new reference to the same Cursor object
cursor.rewind(1);
while (cursor!=lastNote){
}

The 'while' is literally comparing the object to itself so it will never be not-equal.

I think you should be looking at the cursor.segment values as the cursor.next() function will step you through the objects.

-Dale

In reply to by P5music

I tried your qml in the latest MuseScore 3 and of course it trips up on the import "MuseScore 1.0".

Have you considered the latest MuseScore version for your work?

I chimed in because I'm new to MuseScore and needed to fix a desired plug-in for my use. In the course of that I learned a few tricks. But it's all 3.0+.

The problem you encountered was due to the assumption that a cursor.element is a chord. There are many kinds of elements (a rest for example).

Rather then using a text narrative to explain, I've attached your example.qml that I reworked some.

I now have it calling "hisPlugin" (that would be mine) instead of "myPlugin" (that's yours.)

This framework works in the latest MuseScore.

As for all the stuff about nplets etc... you're on your own but this should get you started.

-Dale

Attachment Size
example_2-rework.qml 2.27 KB

In reply to by DLLarson

Thank you.
[I am testing it on MS 2.x because a few months ago I tried the new version but it was laggish and unusable.
Furthermore, I am using Ubuntu Studio, and I found MS 2.x installed by default.]
I still do not understand the segment, however I updated the while cycle with the type check as in your example. (Please see the attachment)

But very strange things happen.

I tested it on 4 triplets and after the plugin run the notes sounds as expected, that is in reverse order within each triplet.
-A 5th triplet is involved too, in the next measure, but it is unwanted effect, the processing exceeds the selection.
-The first 2 triplets are unchanged as it comes to appearance, while the 3rd and 4th triplets are completely altered (notes are spread too much, although they sound as they should).

If I save the score, the next time I open it I see that the appearance has not changed (so the first 2 triplets are in the original form, the 3rd and 4th have the spread notes), but this time the first 2 triplets play the original notes (not reversed) and the 3rd and 4th plays the spread notes (and the 5th too).
It seems that both my algorithm and the plugin mechanism have some problems.

Attachment Size
example.qml 1.32 KB

In reply to by P5music

I'm going to suggest you start a new thread restating the issues and attaching updated readable code.

IMO, you would've been better off migrating your code into my version of your code.

UseThisApproach.png

I suggest migrating your work in small testable steps to help you understand the problem you trying to solve with MuseScore. In the programming biz this is called step-wise refinement.

-Dale

In reply to by DLLarson

Thank you for your suggestion.
I am going to continue this discussion in the other thread if you are so kind to follow me.
I will recapitulate the problem with my script and attach the qml.
Please note that the strangeness I have pointed out goes beyond my simple algorithm and you just have to put myPlugin in your Musescore to see that.
I will try also to insert my algorithm in your code. I hope that your version is such that the problems do not occurr.
But it is also worthy to inspect and test my code, I think, by your side.

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