12 Replies Latest reply on Apr 26, 2016 8:14 AM by K.Daube

    Problems with ObjectValid ()

    K.Daube Level 1

      Dear experts,

      In my scripts I have this snippet:

      var tLoc2, fRange, j, thisMarker, thisId, thatId;
      //...
      for (j = 0; j < markerRange.length; j++)        // loop should not be necessary due to findparms
      {  
      var oTextItem = markerRange[j];  
      thisMarker = oTextItem.obj;                   // should contain only one marker
      }  
      if (!thisMarker.ObjectValid()) {
      Alert ("IndexNearestMarker does not find a " + sMarkerName + " marker after the cursor location, first marker used");
      thisMarker =  markerArray[0];
      }
      thisId = thisMarker.Unique; 
      

      Although thisMarker shows all expected properties, and in the document the found marker is already selected, line 8 reports "thisMarker is undefined".

      data-browser.png

      Replacing line 8 with this

      if (!oTextItem.obj.ObjectValid()) {
      

      the sript runs and line 12 does not create a problem at all !

       

      What is wrong with the original line 86?

        • 1. Re: Problems with ObjectValid ()
          cudspan Level 1

          I would try putting the test inside the FOR loop.  It looks to me as though you're going through all the items in the marker range, and the last item is undefined.  IO know you say it's a range of 1, but FrameMaker might do something strange like add a dummy object at the end of the range.  Text ranges and text items lists can be strange sometimes.

           

          So check the length of markerRange and see if it's what you really expect.  And also, try putting the test inside the FOR loop -- that's probably where it belongs anyway.

           

          Just guessing....  Hope it helps!

          • 2. Re: Problems with ObjectValid ()
            frameexpert Level 4

            What is markerRange in your code?

            • 3. Re: Problems with ObjectValid ()
              Russ Ward Level 4

              Yes, something doesn't seem right overall, but there isn't enough code there to see the problem. If markerRange is an array of text locations, well that seems odd. Why are you creating an array of TextLocs? I think a misunderstanding of what you are doing with markerRange is the cause here.

               

              Russ

              • 4. Re: Problems with ObjectValid ()
                K.Daube Level 1

                Well, I didn't think that the full code is required, because outside of the test (line 12) thisMarker is taken as it should.

                Nevertheless here is the full function:

                function IndexNearestMarker (oDoc, sMarkerName) { // get nearest following marker of type sMarkerName
                // function returns index in corresponding array
                  var markerArray = [], markerRange = [];
                  var tLoc2, fRange, j, thisMarker, thisId, thatId;
                  markerArray = GetMarkerArray (sMarkerName);     // corresponding global marker array
                
                // we are at oDoc.TextSelection and need to find the marker-index in the array.
                  var currentSelection = oDoc.TextSelection;
                  var tLoc2 = oDoc.TextSelection.end;             // after possible selection
                
                  var findParams= GetFindParamsMarker (sMarkerName); // define the marker search
                
                  fRange = oDoc.Find  (tLoc2, findParams);        // finds text range (object pgf), lenght 1
                  markerRange = oDoc.GetTextForRange (fRange, Constants.FTI_MarkerAnchor);  // this is an array
                
                  for (j = 0; j < markerRange.length; j++) {      // loop should not be necessary due to findparms
                    var oTextItem = markerRange[j];  
                    thisMarker = oTextItem.obj;                   // should contain only one marker
                  } 
                //if (!thisMaker.ObjectValid()) {                 //? => thisMarker is undefined
                  if (!oTextItem.obj.ObjectValid()) {
                    Alert ("IndexNearestMarker does not find a " + sMarkerName + " marker after the cursor location, first marker used");
                    thisMarker =  markerArray[0];
                  }
                  thisId = thisMarker.Unique; 
                  for (j = 0; j <  markerArray.length; j++) {     // find the equivalent item in array
                    thatId = markerArray[j].Unique;
                    if (thisId == thatId) {break;}
                  }
                  return j;
                } // --- end GetNearestMarker  
                

                So why is line 20 not a valid test if lines 25 ... correctly creates the desired result?

                • 5. Re: Problems with ObjectValid ()
                  Russ Ward Level 4

                  Klaus,

                   

                  Thanks for posting the code. I think that your use of "markerRange" to describe a TextItems array was throwing us off track. Now that I see the whole thing, your problem does seem unusual. This is a total guess, but maybe I would try this to set thisMarker:

                   

                  var thisMarker = markerRange[j].obj;

                   

                  A TextItems array is an array of arrays, so maybe something goofy is going on there by trying to extract a single array? I don't know. Maybe try that. If you still can't get it to work, I'll test it myself if you post the GetFindParamsMarker() function.

                   

                  Russ

                  • 6. Re: Problems with ObjectValid ()
                    Klaus Göbel Level 3

                    Hi Klaus,

                    in my opinion it has nothing to do with FrameMaker.

                     

                    if markerRange.length = 0, (line 16), the constructor of thisMaker is string.

                     

                    AND: String has no object, so your line 20 will result in undefined.

                     

                    If debugging stops with that error, just type into the console:

                    "markerRange.constructor"

                    I'm sure the result will be "string".

                    • 7. Re: Problems with ObjectValid ()
                      cudspan Level 1

                      So....   HAVE you actually tried testing the marker inside of the for loop instead of after the for loop?

                      • 8. Re: Problems with ObjectValid ()
                        K.Daube Level 1

                        Klaus, at that point it says:

                        markerRange.constructor
                        Result: TextItems()
                        

                         

                        Russ, IMHO your suggestion

                        var thisMarker = markerRange[j].obj;
                        

                        is the same as the two steps

                        var oTextItem = markerRange[j];
                        thisMarker = oTextItem.obj;
                        

                         

                        Placing the test inside the loop creates the same error

                         

                        Please find here: the complete test script and the test document .

                         

                        The strange thing still is, that (refering to my complete script line 74)

                        if (!thisMaker.ObjectValid()) {
                        

                        provides the information in the Data Browser as pictured in my initial post:

                        thisMarker = [object Marker]
                        Marker Text = 3rd calc marker in doc  (this depned where i have placed the cursor intially)
                        

                         

                        My pragmatic solution is this (see full script):

                        - do not use line 74

                        - but use line 75 for the test

                         

                        Nevertheless Your research will clarify what's really going on here. Thank You all.

                        • 9. Re: Problems with ObjectValid ()
                          Klaus Göbel Level 3

                          Hi Klaus,

                          your "comlete test script" doesn't run because there are only functions in it.

                           

                          I'm looking forward to test this script and find the problem. Could you please post the whole script.

                          thanks

                          Klaus

                          • 10. Re: Problems with ObjectValid ()
                            K.Daube Level 1

                            Sorry for that!

                            Too many experiments at a time...

                            The link now points to the correct test script.

                             

                            Klaus

                            • 11. Re: Problems with ObjectValid ()
                              Klaus Göbel Level 3

                              Hi Klaus,

                               

                              I was looking up to the sky, instead of looking to the tip of my nose.

                               

                              The solution is sooooo simple: a typo in line 20

                               

                              thisMaker -> thisMarker

                               

                               

                              • 12. Re: Problems with ObjectValid ()
                                K.Daube Level 1

                                Embarrassing!

                                Ash on me!