2 Replies Latest reply on Jun 3, 2012 3:33 AM by [Jongware]

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



      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:


      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

        • 1. Re: [JS][CS5]Determine if Object is Subclass or Instance of
          Marc Autret Level 5

          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.




          • 2. Re: [JS][CS5]Determine if Object is Subclass or Instance of
            [Jongware] Most Valuable Participant

            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.