2 Replies Latest reply on Oct 8, 2010 2:41 AM by Marc Autret

    [JS/CS5] Get multiple by name?

    Pontus Uggla Level 1

      I have changed my script it will work in CS5, putting the reference in the name and not the label. But now when I try to get multiple items by name it only returns one.




      This would return all items with the label 'name' in CS4, but it only returns one item with the name 'name' in CS5.


      Any way to get them all?

        • 1. Re: [JS/CS5] Get multiple by name?
          Marc Autret Level 4

          Yes, it's an important change in CS5.


          The Collection.item(/*string*/) method is a shortcut for Collection.itemByName(/*string*/) AND it expects the name of your component, which is assumed to be unique. So, technically, setting the same name for multiple items is not an option, because itemByName() will return a ‘non-collective specifier’. As soon as a receiver is found having the requested name, the operation is completed and .getElements() returns a singleton array.


          To retrieve all the wanted objects, you now must loop though everyItem() and filter the name by yourself:


          var pgItems = app.activeDocument.pageItems.everyItem().getElements(),
               retItems = [];

          while( pgItem=pgItems.pop() )
               (pgItem.name == 'query_name') && retItems.push(pgItem);


          alert( retItems.length );


          It probably has performance cost on heavy documents. If possible, use a more specific collection if you know what kind of objects you are targetting (e.g. textFrames).




          • 2. Re: [JS/CS5] Get multiple by name?
            Pontus Uggla Level 1

            This was what i came up with, but i just ask why?


            Why did they remove such a good thing. What I did now was to only search one spread since i sometimes work with houndreds of pages.


            I didn´t mind the changes very much, the layer thing is good and visual, but why not make the getElements() work the same way?