• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to identify constructor of PageItems?

Participant ,
Apr 12, 2020 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!

TOPICS
Scripting

Views

1.5K

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
community guidelines

correct answers 2 Correct answers

Community Expert , Apr 13, 2020 Apr 13, 2020

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

 

 

 

Votes

Translate

Translate
Community Expert , Apr 13, 2020 Apr 13, 2020

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

Votes

Translate

Translate
Community Expert ,
Apr 12, 2020 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

 

Votes

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
community guidelines
Participant ,
Apr 13, 2020 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()

 

 

Votes

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
community guidelines
Participant ,
Apr 13, 2020 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…

Votes

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
community guidelines
Guide ,
Apr 13, 2020 Apr 13, 2020

Copy link to clipboard

Copied

Have looked at:

 

obj.constructor.name

 

P.

Votes

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
community guidelines
Participant ,
Apr 13, 2020 Apr 13, 2020

Copy link to clipboard

Copied

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

Votes

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
community guidelines
Community Expert ,
Apr 13, 2020 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

Votes

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
community guidelines
Participant ,
Apr 13, 2020 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.

Votes

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
community guidelines
Community Expert ,
Apr 13, 2020 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"
        } 
    };   
} 

 

Votes

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
community guidelines
Participant ,
Apr 13, 2020 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.

Votes

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
community guidelines
Community Expert ,
Apr 13, 2020 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

 

 

 

Votes

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
community guidelines
Participant ,
Apr 13, 2020 Apr 13, 2020

Copy link to clipboard

Copied

That's it! Thanks so much.

Votes

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
community guidelines
Community Expert ,
Apr 13, 2020 Apr 13, 2020

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:

convertShape()-1.PNG

 

Check the Layers panel, the generic name of the object is <circle> ( <Kreis> in my German InDesign 😞

 

convertShape()-2.PNG

 

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

 

convertShape()-3.PNG

 

Regards,
Uwe Laubender

( ACP )

 

Votes

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
community guidelines
Participant ,
Apr 13, 2020 Apr 13, 2020

Copy link to clipboard

Copied

Oh, nice Uwe. Very useful to know, many thanks and frohe Ostern!

Votes

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
community guidelines
Community Expert ,
Apr 14, 2020 Apr 14, 2020

Copy link to clipboard

Copied

LATEST

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 )

Votes

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
community guidelines