Skip navigation
Home/Support/

Forums

893 Views 6 Replies Latest reply: Sep 2, 2009 5:18 AM by r_olsen RSS
r_olsen Calculating status... 55 posts since
Apr 27, 2009
Currently Being Moderated

Sep 1, 2009 5:03 AM

Match pageItemX from array1 with pageItemX from array2?

Cheers,


I've got a spread with one page. Two page objects (a polygon and an rectangle) on the page, and one rectangle object outside the page. I'm trying to find the object not associated with the page, using two arrays: array1 containing doc.spread.allPageItems, and array2 containing doc.page[counter].allPageItems.


So I go look for the object using for in loops. But no matter what I try I'm only able to find the two objects the arrays have in common. Sigh


Looking for a little javascript assistance..

     

        var oDoc = app.activeDocument;
        var spreadItems = [];
        var pageItems = [];
        var DeleteTheseItems = [];
        
        // loop through spreads
        for ( var aCounter = 0; aCounter < oDoc.spreads.length; aCounter++ )
        {
            var oSpread = oDoc.spreads[aCounter];    
            
            // loop through spread items
            for ( var bCounter = 0; bCounter < oSpread.allPageItems.length; bCounter++ )
            {                
                var oSpreadItem = oSpread.allPageItems[bCounter];
                spreadItems.push(oSpreadItem); // 3 objects gets pushed
            }                
                // loop through spreads pages
                for ( var cCounter = 0; cCounter < oSpread.pages.length; cCounter++ )
                {
                    var oPage = oSpread.pages[bCounter];
                    
                    // loop through page items
                    for ( var dCounter = 0; dCounter < oPage.allPageItems.length; dCounter++ )
                    {
                        var oPageItem = oPage.allPageItems[dCounter];
                        pageItems.push( oPageItem ); // 2 objects gets pushed                        
                    } // cCounter
                } // bCounter                
        } // aCounter
    
        alert("spreadItems: "+spreadItems.toSource()); // 3 objects total: 1 outside page, 2 on the page
        alert("pageItems: "+pageItems.toSource()); // 2 objects on page
        
        for ( spreadItem in spreadItems )
        {            
            for ( pageItem in pageItems )
            {                
                if ( spreadItems[spreadItem]  == pageItems[pageItem] )
                {
                    DeleteTheseItems.push(spreadItems[spreadItem]);  // the 2 objects in common gets pushed
                    break;
                }
            }
        }
    
        alert(DeleteTheseItems.toSource())     
 
  • Marc Autret Participant 382 posts since
    Jun 12, 2009

    Some remarks and suggestions:

     

    - why don't you use the <item>.parent.constructor property to determine if the thing you're checking is specifically a Spread item ?

    Traversing mySpread.allPageItems, you just need to find the element(s) which satisfy (<item>.parent.constructor == Spread)

    [or MasterSpread]

     

    - in your code, you build two arrays (spreadItems and pageItems)  from scratch, but why?

    oSpread.allPageItems and oPage.allPageItems will return exactly the same things: the allPageItems prop returns an Array object [contrary to the pageItems one, which returns a PageItems collection].

     

    - the ( spreadItems[spreadItem]  == pageItems[pageItem] ) test does not work because the == operator does not work on Objects. (I mean, on Object instances)

    I suggest you to try rather id comparison: ( myObjRef1.id == myObjRef2.id )

     

    @+

    Marc

  • Marc Autret Participant 382 posts since
    Jun 12, 2009

    Anyway, invoking allPageItems is useless (performance costing) when you want to test first-level page items.

     

    So, here is a possible fix for your script:

    var pItems = app.activeDocument.pageItems.everyItem().getElements();
    var exclusiveSpreadItems = [];
     
    for ( var p, i = pItems.length-1 ; i>=0 ; i-- )
         {
         p = pItems[i].parent.constructor;
         if ( p == Spread || p == MasterSpread )
              exclusiveSpreadItems.push(pItems[i]);
         }
     
    alert(exclusiveSpreadItems);
    

     

    @+

    Marc

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points