Copy link to clipboard
Copied
Hi there,
I am currently trying to implement a tool for inspecting all sorts of data types and their properties using the reflect() method. While playing around with the reflection results, I noticed that normal Arrays return number index properties (0, 1, 2 etc.) as one would expect, but for some reason special Arrays that hold all items of a container, like allPageItems, allTextFrames etc. do not contain these number properties, although they appear to have a length greater than 0.
See this example:
// @target InDesign
var testArray = ["foo", "bar", "baz"];
$.writeln(testArray); // --> foo,bar,baz
$.writeln(testArray.length); // --> 3
$.writeln(testArray.reflect.properties); // --> 0,1,2,length,__proto__
$.writeln("-----------------------");
var doc = app.documents.add();
doc.rectangles.add();
doc.textFrames.add();
doc.ovals.add();
$.writeln(doc.zeroPoint); // --> 0,0
$.writeln(doc.zeroPoint.length); // --> 2
$.writeln(doc.zeroPoint.reflect.properties); // --> 0,1,length,__proto__
$.writeln("-----------------------");
$.writeln(doc.allPageItems); // --> [object Oval],[object TextFrame],[object Rectangle]
$.writeln(doc.allPageItems.length); // --> 3
$.writeln(doc.allPageItems.reflect.properties); // --> length,__proto__
As you can see the last array (allPageItems returns a length of three, but does not return properties like 0, 1, 2, unlike the other Arrays.
Does anyone know why that is? Would be great, if I could just make use of reflection as I can with the normal Arrays.
Thanks for your help!
TR
P.S.: InDesign CS6, OS X 10.8.5.
var coll = app.activeDocument.allPageItems
var arr = Array.prototype.slice.call( coll, 0 );
quickNdirty to convert collections to arrays
Copy link to clipboard
Copied
Just a wild guess here, but those "allPageItems" and all are not Js arrays but array-like objects returned by the underlying C++ code in Indesign. They are basically "uninitialised" by the JS engine.
Copy link to clipboard
Copied
var coll = app.activeDocument.allPageItems
var arr = Array.prototype.slice.call( coll, 0 );
quickNdirty to convert collections to arrays
Copy link to clipboard
Copied
var testarray=app.activeDocument.allPageItems;
$.writeln(testarray.reflect.properties); // --> length,__proto__
testarray=Array.prototype.slice.call( testarray, 0 );
$.writeln(testarray.reflect.properties); // --> 0,1,2,length,__proto__
-hans-: AWESOME
Copy link to clipboard
Copied
Great workaround Hans, works like a charm. Thank you very much!
Regards,
TR
Copy link to clipboard
Copied
Hi colleagues,
My two pennies:
var a = [].concat(app.activeDocument.allPageItems);
instantly set a clean Array that will pass the reflect test as well.
Anyway, it's worth pointing out that a=app.activeDocument.allPageItems already is an Array object, not a collection. There is likely a bug in a.reflect.properties—in the sense MasterDomino illustrated—, but one can check that:
• a.constructor===Array,
• a.reflect.methods returns all methods we expect from an Array.
So, unless your script actually involves the Reflection object, you can handle app.activeDocument.allPageItems as such, it is indeed an array.
@+
Marc