Copy link to clipboard
Copied
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())
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.parent.constructor;
if ( p == Spread || p == MasterSpread )
exclusiveSpreadItems.push(pItems);
}
alert(exclusiveSpreadItems);
@+
Marc
Copy link to clipboard
Copied
I've boiled it down to this. I want c and e pushed, but not d.
var array1 = ['c','d','e'];
var array2 = ['a','b','d'];
var array3 = [];
var found = false;
for( var index1 in array1 )
{
for( var index2 in array2 )
{
if ( array1[index1] != array2[index2] )
{
array3.push(array1[index1]);
//found = true;
break;
}
}
/*
if ( found )
break;
*/
}
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.parent.constructor;
if ( p == Spread || p == MasterSpread )
exclusiveSpreadItems.push(pItems);
}
alert(exclusiveSpreadItems);
@+
Marc
Copy link to clipboard
Copied
Hi Marc,
Thank you for an incredible helpful reply. I'm working on a little cleanup script to remove objects with not on a page (page dimensions + a userdefined offset). So far I've been trying to compare page.allPageItems and spread.allPageItems along with the coordinates of the items. I'm blown away by your simple approach. Compared to my own many many confusing lines this is just perfect. Thanks!
Copy link to clipboard
Copied
Script is working. Any comments/tips/suggestions?
var oDoc = app.activeDocument;
var oArray = [];
oArray["height"] = roundNumber ( Number (oDoc.documentPreferences.pageHeight) );
if ( oDoc.documentPreferences.documentBleedTopOffset != undefined ? oArray["bleed"] = roundNumber ( Number (oDoc.documentPreferences.documentBleedTopOffset) ) : 0 );
if ( oDoc.documentPreferences.slugTopOffset != undefined ? oArray["slug"] = roundNumber ( Number (oDoc.documentPreferences.slugTopOffset) ) : 0 );
var oDialog = app.dialogs.add( {name: "Cleanup document", canCancel: true} );
with ( oDialog )
{
with ( dialogColumns.add() )
{
with ( borderPanels.add() )
{
staticTexts.add( {staticLabel:"Remove objects outside:"} )
var myBleedCheck = checkboxControls.add( {staticLabel: "Bleed", checkedState: false} );
var mySlugCheck = checkboxControls.add( {staticLabel: "Slug", checkedState: false} );
}
with ( borderPanels.add() )
{
staticTexts.add( {staticLabel: "Custom entry:"} )
var myCustomEntry = realEditboxes.add();
with (myCustomEntry)
{
staticTexts.add( {staticLabel: "mm."} );
}
}
}
with ( dialogColumns.add() )
{
staticTexts.add( {staticLabel:"Rasmus Olsen"} );
staticTexts.add( {staticLabel:"Version 1.0"} );
}
}
showUI ( oDoc, oDialog, oArray );
function showUI ( oDoc, oDialog, oArray )
{
if ( oDialog.show() == true )
{
if ( myBleedCheck.checkedState )
{
alert( "Parsing bleed" );
cleanup ( oDoc, oArray, oArray["bleed"] );
oDialog.destroy();
}
else if ( mySlugCheck.checkedState )
{
alert( "Parsing slug" );
cleanup ( oDoc, oArray, oArray["slug"] );
oDialog.destroy();
}
else if ( myCustomEntry.editValue > 0 )
{
alert( "myCustomEntry.editValue: "+myCustomEntry.editValue );
cleanup ( oDoc, oArray, myCustomEntry.editValue );
oDialog.destroy();
}
else
{
alert("else");
oDialog.destroy();
}
}
}
function cleanup( oDoc, oArray, offset )
{
var pItems = oDoc.pageItems.everyItem().getElements();
oArray["height"] = roundNumber ( Number (oDoc.documentPreferences.pageHeight) );
oArray["width"] = roundNumber ( Number (oDoc.documentPreferences.pageWidth*parseInt(oDoc.pages.length)) );
var exclusiveSpreadItems = [];
for ( var p, i = pItems.length-1 ; i>=0 ; i-- )
{
p = pItems.parent.constructor;
if ( p == Spread || p == MasterSpread )
exclusiveSpreadItems.push(pItems);
}
for ( var j = exclusiveSpreadItems.length-1 ; j>=0 ; j-- )
{
var oPageItem = exclusiveSpreadItems
oArray["CordY1"] = roundNumber ( oPageItem.geometricBounds[0] );
oArray["CordX1"] = roundNumber ( oPageItem.geometricBounds[1] );
oArray["CordY2"] = roundNumber ( oPageItem.geometricBounds[2] );
oArray["CordX2"] = roundNumber ( oPageItem.geometricBounds[3] );
if (
// Catch object to the left
oArray["CordX2"] <= -(offset)
||
// Catch objects at the top
oArray["CordY2"] <= -(offset)
||
// Catch objects to the right
oArray["CordX1"] >= (oArray["width"]+offset)
||
//Catch objects below
oArray["CordY1"] >= (oArray["height"]+offset)
)
{
try
{
oPageItem.remove();
}
catch( error ) {} // catch silently
}
}
}
function roundNumber ( number ) { return Math.round ( number ); }
Copy link to clipboard
Copied
Hmm, setting the width was not working. Here's an update: Width is now dynamically adjusting according to parent spreads number of pages.
function cleanup( oDoc, oArray, offset )
{
var pItems = oDoc.pageItems.everyItem().getElements();
var exclusiveSpreadItems = [];
for ( var p, i = pItems.length-1 ; i>=0 ; i-- )
{
p = pItems.parent.constructor;
if ( p == Spread || p == MasterSpread )
{
exclusiveSpreadItems.push(pItems);
}
}for ( j = exclusiveSpreadItems.length-1 ; j>=0 ; j-- )
{
var oPageItem = exclusiveSpreadItems;
var oParent = oPageItem.parent;
oArray["width"] = roundNumber ( Number (oDoc.documentPreferences.pageWidth*parseInt(oParent.pages.length)) );
oArray["CordY1"] = roundNumber ( oPageItem.geometricBounds[0] );
oArray["CordX1"] = roundNumber ( oPageItem.geometricBounds[1] );
oArray["CordY2"] = roundNumber ( oPageItem.geometricBounds[2] );
oArray["CordX2"] = roundNumber ( oPageItem.geometricBounds[3] );
if (
// Catch object to the left
oArray["CordX2"] <= -(offset)
||
// Catch objects at the top
oArray["CordY2"] <= -(offset)
||
// Catch objects to the right
oArray["CordX1"] >= (oArray["width"]+offset)
||
//Catch objects below
oArray["CordY1"] >= (oArray["height"]+offset)
)
{
try
{
oPageItem.remove();
}
catch( error ) {}
}
}
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now