Highlighted

How to identify constructor of PageItems?

Explorer ,
Apr 12, 2020

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!

Adobe Community Professional
Correct answer by rob day | Adobe Community Professional

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

 

 

 

Topics

Scripting

Views

291

Likes

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

How to identify constructor of PageItems?

Explorer ,
Apr 12, 2020

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!

Adobe Community Professional
Correct answer by rob day | Adobe Community Professional

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

 

 

 

Topics

Scripting

Views

292

Likes

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
Adobe Community Professional ,
Apr 12, 2020

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

 

Likes

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
Reply
Loading...
Explorer ,
Apr 13, 2020

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()

 

 

Likes

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
Reply
Loading...
Explorer ,
Apr 13, 2020

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…

Likes

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
Reply
Loading...
Advisor ,
Apr 13, 2020

Copy link to clipboard

Copied

Have looked at:

 

obj.constructor.name

 

P.

Likes

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
Reply
Loading...
Explorer ,
Apr 13, 2020

Copy link to clipboard

Copied

Yeah, that was my initial thought, but it returns PageItem, not the actual object type.

Likes

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
Reply
Loading...
Adobe Community Professional ,
Apr 13, 2020

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

Likes

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
Reply
Loading...
Explorer ,
Apr 13, 2020

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.

Likes

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
Reply
Loading...
Adobe Community Professional ,
Apr 13, 2020

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:

 

Screen Shot 9.pngScreen Shot 10.png

 

 

 

//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"
        } 
    };   
} 

 

Likes

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
Reply
Loading...
Explorer ,
Apr 13, 2020

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.

Likes

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
Reply
Loading...
Adobe Community Professional ,
Apr 13, 2020

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

 

 

 

Likes

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