0

I'm a beginning learner of InDesign scripting and would like to help myself with debugging, but my attempts seem to run into walls. Hope someone has some insights that will help me going forward.

I'm working on a little project that loops through some selected tables, puts the 3 tables into an array/variable (accomplished that) and then loops through the content of those tables to find a GREP match and store those in an array/variable (for further uses I won't get into now)

My main objective at this point: See exactly what text characters the .findGrep(); function is catching and display those in the Javascript Console of the ExtendScript Toolkit app.

So here's a bit of the journey up to this point, including codes tried and suggestions from others. (All of my attempted uses of these has failed...why I'm here now... and why this is long; my apologies)

Initial try. var myTables = []; (in Data Browser this shows values of [object Table], [object Table], [object Table]

var myFinds = [];
var myTest = [];
var myCharacters = [];
app.findGrepPreferences = null;    
app.findGrepPreferences.findWhat = "\"";    
    for (x = 0; x < myTables.length; x++) {
        var myFinds = myTables[x].findGrep();
        $.writeln(myFinds);
    };

Notes on this code: Because not every table has the characters in the findWhat, sometimes in this loop myFinds has nothing, but when it does have something, it shows this in console [object Character],[object Character],[object Character]

So someone (firstHelp) gave me this: And it did not work... error thrown on .contents.toString(); *"undefined is not an object" which I thought, "ok, yes I see at times in the loop myFinds has nothing in it... more on this later"

var stringArray = [];
for( var n=0; n<myFinds.length; n++ ) {
stringArray[n] =  myFinds[n].contents.toString();
};
$.writeln(myFinds.join("\r"));

Code revamp Gave up on the $.writeln(myFinds); within the loop and tried this in order to gather Grep finds in a variable/array that could be dealt with outside of loop.

for (x = 0; x < myTables.length; x++) {
$.writeln(myTables[x].cells.firstItem().texts[0].contents[0]);
myFinds.push(myTables[x].findGrep());
 };
$.writeln(myFinds);

ExtendScript Toolkit console now showing this for myFinds:

*myFinds = [Array], [object Character], [object Character], [object...
+ (object symbol) 0 =
+ (object symbol) 1 = [object Character], [object Character], [object Character]
+ (object symbol) 2 =
+ (object symbol) _proto_ =*

*again tried the .contents.toString(); on the myFinds and still the same error, "undefined..." including targeting the array when it clearly had something in it.

**So then I get this tipoff...(but no helpful code to apply to what I already have)

"you are dealing with arrays of arrays mixed with texts.

So you have to check with each item of the result array if it is text or another array of texts.

If it is an array loop that array." And later this bit of code that is supposed to "flatten" my array... a = [].concat.apply([],a); Replacing a with myFinds like this, myFinds = [].concat.apply([],myFinds); did absolutely nothing. The array and its contents showed no change in the console... and I have no idea how to loop through each item of this array within an array, find out if it's text or another array and then show its real contents to console.

Really...how many loops and if/thens etc do I need to run on one array to show its actual contents in the console? But I know I struggle with breaking down every little step I want, to its minute scripting granularity and so my ignorance regularly impedes me. I welcome any suggestions/tips to move me closer to my **main objective" as stated above. Thanks

A Zook
  • 37
  • 7

1 Answers1

0

Regarding the first help. The real reason why you get an error while accessing content property is that you don’t check the type of the object and presume it will be a Text object. As the findGrep may not find a Text occurrence, you actually get an empty array. And Array.prototype.contents doesn’t exist hence the error. Then $.writeln is legacy of Adobe ExtendScript toolkit, the IDE for ExtendScript. This product is no longer de eloped and maintained by Adobe. You should consider using other logging techniques such as the Visual Studio ExtendScript plugin which will allow you to use breakpoints and everything you need.

Narendranath Reddy
  • 3,833
  • 3
  • 13
  • 32
Loic
  • 1
  • Thank you Narendranath. I will look into the Visual Studio tip: looks like the way to go. To your point here: "...you don’t check the type of the object and presume it will be a Text object." I assume that's accomplished with an if/then? How would I do that? I have no idea how to "check the type of the object". It sounds like a simple snippet would do it, but maybe it's more involved. I have no idea. – A Zook Dec 05 '19 at 12:25