Skip navigation
ted42
Currently Being Moderated

[JS][CS5]Determine if Object is Subclass or Instance of

Jun 3, 2012 2:33 AM

Tags: #cs5 #script #javascript #indesign_cs5 #scripting #scriptui

Hi,

I am trying to loop over all Layeritems and want to take an action, depending on the type of the item.

 

While I learned about several ways to determin the exact type, I would like to determin weather the Item might be either an Instance of the class or be a subclass. For example:

 

Graphic
EPS | Image | ImportedPage | PDF | PICT | WMF

I know have the following:

 
layer = app.activeDocument.layers[layerIndex];

for (i = 0; i < layer.allPageItems.length; i++) {
    x = layer.allPageItems[i]
    if(x instanceof Graphic) {
       alert('I wont tell you if a subtype (like Image) has been selected');
       //I actually want this to trigger for graphic and all its subtypes (eps,image,pdf,...)     } }

I tryed to figure out the syntax of isPrototypeOf but I cant get it to work

Graphic.isPrototypeOf (x)

how do I go about to do this ( I know there is allGraphics but I want to be able to do this for other types too)  Greetings ted

 
Replies
  • Currently Being Moderated
    Jun 3, 2012 3:32 AM   in reply to ted42

    Hi Ted,

     

    What the DOM claims to be a "base class" or a subclass does not exactly meet the JS rules regarding instanceof or isPrototypeOf, due to the specific nature of object specifiers and the way they are resolved. To my mind, PageItem or Graphic may be regarded as _virtual classes_ (interfaces?) for temporary specifiers that are not resolved yet. Once a specifier is resolved—which is automatically done through arrays like allPageItems or allGraphics—it simply takes its own type (see x.__class__) and there is no more connection with the _virtual_ base class. Therefore, to find whether a resolved object is a kind of Graphic, I usually check if it addresses a specific property only shared by graphics, e.g.:

     

    if( x.properties.hasOwnProperty('imageTypeName') ){...}

     

    Then, of course, you can use switch(x.__class__){...} or switch(x.constructor){...} to refine treatments.

     

    Hope that helps.

     

    @+

    M

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 3, 2012 3:33 AM   in reply to ted42

    That's a familiar one :)

     

    Harbs's suggestion in post #6 in http://forums.adobe.com/message/4080971?tstart=0 should work for you. You may safely skip over the Oval/Rectangle discussion, but somewhere in that thread there is a link to Marc Autret's blog on getElements, enlightening stuff which you might want to read.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points