Skip to main content
K.Daube
Community Expert
Community Expert
April 25, 2016
Answered

Problems with ObjectValid ()

  • April 25, 2016
  • 2 replies
  • 1252 views

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
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".

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?

This topic has been closed for replies.
Correct answer Klaus Göbel

Sorry for that!

Too many experiments at a time...

The link now points to the correct test script.

Klaus


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

2 replies

frameexpert
Community Expert
Community Expert
April 25, 2016

What is markerRange in your code?

www.frameexpert.com
Inspiring
April 25, 2016

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!

Legend
April 25, 2016

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

K.Daube
Community Expert
K.DaubeCommunity ExpertAuthor
Community Expert
April 25, 2016

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
    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.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?