Skip navigation
r_olsen
Currently Being Moderated

Match pageItemX from array1 with pageItemX from array2?

Sep 1, 2009 5:03 AM

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())     
 
  • Currently Being Moderated
    Community Member
    Sep 1, 2009 8:34 AM

    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

    |
    Mark as:
  • Currently Being Moderated
    Community Member
    Sep 1, 2009 8:57 AM

    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

    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

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