Copy link to clipboard
Copied
Hi all,
Let's assume a group contains 3 objects. How can I identify what type each object it is without iterating through all possible results in a single line? Since the type of object is unknown, I can only directly address it with pageItem, but how can I find out the actual object type? Both group[0].pageItem[0].toSource() and toSpecifier() give me pageItem as a result.
Many thanks and Happy Easter!
Try allPageItems rather than pageItems. My group with 3 items example returns this:
$.writeln(app.activeDocument.selection[0].allPageItems[0].constructor.name)
//returns Oval
$.writeln(app.activeDocument.selection[0].allPageItems[1].constructor.name)
//returns TextFrame
$.writeln(app.activeDocument.selection[0].allPageItems[2].constructor.name)
//returns Rectangle
Hi Frank,
there is a direct way to resolve a page item:
// That will return [object PageItem]
var myPageItem = app.documents[0].pageItems[0];
// That will return whatever this is:
var whatItReallyIs = app.documents[0].pageItems[0].getElements()[0];
FWIW: Usually in a selection with app.selection the item is already resolved.
Warning!
There could be false positives after the user or a scripter used the function
convertShape()
var doc = app.documents.add();
var rectangle = doc.rectangles.a
...
Copy link to clipboard
Copied
Following, but I don't believe there's a way to get at it without testing whether it's an instanceof a particular child class.
if (groups[0].pageItems[0] instanceof Graphic) {
//do graphic stuff
}
else if (groups[0].pageItems[0] instanceof TextFrame) {
//do textframe stuff
}
//etc
Copy link to clipboard
Copied
you can get objects' array of the same type
group[0].textFrames.everyItem().getElements()
group[0].rectangles.everyItem().getElements()
group[0].graphicLines.everyItem().getElements()
Copy link to clipboard
Copied
Ok, thanks guys. Looks like there is no 'quick' way of finding out. Really strange as I thought a simple method could do it…
Copy link to clipboard
Copied
Have looked at:
obj.constructor.name
P.
Copy link to clipboard
Copied
Yeah, that was my initial thought, but it returns PageItem, not the actual object type.
Copy link to clipboard
Copied
.valueOf() returns the object type:
//a selection
var p=app.activeDocument.selection[0].valueOf();
//check if the object class of the selection is a rectangle, if not return the class
if (p == "[object Rectangle]") {
$.writeln("The Selected Object is a Rectangle")
} else {
$.writeln(p)
}
But, Pickory’s constructor.name should also work. If my selection is a rectangle
$.writeln(app.activeDocument.selection[0].constructor.name)
//returns Rectangle
Copy link to clipboard
Copied
obj.pageItems[0].valueOf() returns [object PageItem] (the same as just using obj.pageItems[0]) I'm afraid… which requires me to if-check through all possible object types, which I wanted to avoid.
Copy link to clipboard
Copied
Sorry if I‘m not understanding, but do you want to loop thru all of the objects inside of a group and check the items’ class?
So here I’m changing the fill of the oval and skipping the others:
//a selected group
var g = app.activeDocument.selection[0];
if (g.constructor.name == "Group") {
var api = g.allPageItems;
for (var i = 0; i < api.length; i++){
if (api[i].constructor.name == "Oval") {
api[i].fillColor = "Black"
}
};
}
Copy link to clipboard
Copied
I'd like to avoid using if statements, becasue it means I have to check all possible object types. The pageItem could be any type of element, so I'd like to use something like: obj.pageItems[0].objectType() which returns for example 'Oval' instead of PageItem. But the objectType() doesn't seem to exist.
I'm not trying to verify if my PageItem is a specific object type, that would be an simply and short if statement as per your example. I just want to know what type of element my PageItem is.
Sorry if this sounds confusing.
Copy link to clipboard
Copied
Try allPageItems rather than pageItems. My group with 3 items example returns this:
$.writeln(app.activeDocument.selection[0].allPageItems[0].constructor.name)
//returns Oval
$.writeln(app.activeDocument.selection[0].allPageItems[1].constructor.name)
//returns TextFrame
$.writeln(app.activeDocument.selection[0].allPageItems[2].constructor.name)
//returns Rectangle
Copy link to clipboard
Copied
That's it! Thanks so much.
Copy link to clipboard
Copied
Hi Frank,
there is a direct way to resolve a page item:
// That will return [object PageItem]
var myPageItem = app.documents[0].pageItems[0];
// That will return whatever this is:
var whatItReallyIs = app.documents[0].pageItems[0].getElements()[0];
FWIW: Usually in a selection with app.selection the item is already resolved.
Warning!
There could be false positives after the user or a scripter used the function
convertShape()
var doc = app.documents.add();
var rectangle = doc.rectangles.add({ geometricBounds : [ 0, 0, 100 , 100 ]});
doc.pageItems[0]; // will return [object PageItem]
doc.pageItems[0].getElements()[0]; // will return [object Rectangle]
rectangle.convertShape( ConvertShapeOptions.CONVERT_TO_OVAL );
// NOW PREPARE FOR A SURPRISE:
alert( doc.pageItems[0].getElements()[0] ); // STILL returns [object Rectangle]
After running the code above:
Check the Layers panel, the generic name of the object is <circle> ( <Kreis> in my German InDesign 😞
There is no way in the Scripting DOM to get this generic name ( at least I do not know one ).
Even if you select the object you will get the wrong result:
var doc = app.documents.add();
var rectangle = doc.rectangles.add({ geometricBounds : [ 0, 0, 100 , 100 ]});
doc.pageItems[0]; // will return [object PageItem]
doc.pageItems[0].getElements()[0]; // will return [object Rectangle]
rectangle.convertShape( ConvertShapeOptions.CONVERT_TO_OVAL );
// NOW PREPARE FOR A SURPRISE:
alert( doc.pageItems[0].getElements()[0] ); // STILL returns [object Rectangle]
// IF YOU SELECT IT:
doc.select( doc.pageItems[0] );
// YOU ALSO SEE THE WRONG RESULT:
alert( app.selection[0].constructor.name );
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Oh, nice Uwe. Very useful to know, many thanks and frohe Ostern!
Copy link to clipboard
Copied
Hi Frank,
I did not mention the meanest thing about convertShape() objects:
Even if you export to IDMS and place the IDMS snippet you still get the wrong constructor name.
Just tried this…
Regards,
Uwe Laubender
( ACP )