Why does grouping or moving textboxes break the script?

I am a beginner at writing scripts for InDesign (just started today!). I have somehow managed to write a script which auto-fills a text box with a list of the linked images within the document. However, when I group the text box with other objects or move it to another indesign doc, the script no longer works. I get an error that says “Object is invalid” with the source being “resFrame.contents = res.String;”. Can someone please explain why this is happening and how it can be fixed?

Code below…

myDoc = app.activeDocument;  
myPages = myDoc.pages.everyItem().getElements();  
myDoc.viewPreferences.horizontalMeasurementUnits = myDoc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.INCHES;  

for (var k = 0; k < myPages.length; k++) {  
    resString = myDoc.links.everyItem().name.join("\r");  
    resFrame = myFramelinks(myPages[k]);  
    resFrame.contents =  resString;
    }  

function myFramelinks (page) {
    var mFlinks = page.textFrames.item("linkslist");
    return mFlinks;
    }

Answer

Where does your items name “linkslist” come from? By setting a name for the textFrame?

The “name” property of a textframe seems to be persistent for the document. Also if you copy paste the textframe to another doc it still is there.

But if you have a page with 2 textframes and you run:

$.writeln(app.activeDocument.pages[0].textFrames.length);

it will return 2

Group these textframes together and it will return 0

If you want to access elements that are grouped you need to access them like this:

$.writeln (app.activeDocument.pages[0].groups[0].textFrames[0].name)

You also can access them this way:

$.writeln(app.activeDocument.pages[0].allPageItems.length)

This will return 3.

if you run:

for(var i = 0; i < app.activeDocument.pages[0].allPageItems.length; i++){
  $.writeln(app.activeDocument.pages[0].allPageItems[i].constructor.name)
}

The output will be:

Group
TextFrame
TextFrame

So you could create a check for your name and access that particular textFrame again.

(Tested in ID CC 2014 on OSX 10.10.2)

Update:

To retrieve the textframe with the name “linkslist” you should do something like this:

function get_linklist(page) {
  var tf = null;
  for (var i = 0; i < page.allPageItems.length; i++) {
    var item = page.allPageItems[i];
    if (item.name == "linkslist") {
      tf = item;
      break;
    }
  }
  return tf;
}

var doc = app.activeDocument;
var p = doc.pages[0];

var res = get_linklist(p);
if (res === null) {
  $.writeln(" :(");
} else {
  $.writeln(" :)");
}

Attribution
Source : Link , Question Author : Emily , Answer Author : fabianmoronzirfas

Leave a Comment