• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Problems with ObjectValid ()

Community Expert ,
Apr 25, 2016 Apr 25, 2016

Copy link to clipboard

Copied

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

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?

TOPICS
Scripting

Views

756

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Apr 26, 2016 Apr 26, 2016

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

Votes

Translate

Translate
Explorer ,
Apr 25, 2016 Apr 25, 2016

Copy link to clipboard

Copied

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Apr 25, 2016 Apr 25, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 25, 2016 Apr 25, 2016

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Apr 25, 2016 Apr 25, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 25, 2016 Apr 25, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 26, 2016 Apr 26, 2016

Copy link to clipboard

Copied

Klaus, at that point it says:

markerRange.constructor
Result: TextItems()

Russ, IMHO your suggestion

var thisMarker = markerRange.obj;

is the same as the two steps

var oTextItem = markerRange;
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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 26, 2016 Apr 26, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 26, 2016 Apr 26, 2016

Copy link to clipboard

Copied

Sorry for that!

Too many experiments at a time...

The link now points to the correct test script.

Klaus

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 26, 2016 Apr 26, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 26, 2016 Apr 26, 2016

Copy link to clipboard

Copied

LATEST

Embarrassing!

Ash on me!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 25, 2016 Apr 25, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 25, 2016 Apr 25, 2016

Copy link to clipboard

Copied

What is markerRange in your code?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines