Copy link to clipboard
Copied
Hi,
With this post I would like to ask a question.
When working with an array of page items, I'm having very strange issues.
I need to loop over all page items and when the script label of the page item matches some specifications, I need to move them to another layer. Finding the page items I need and putting them in an array is no problem. But when I start to move the items to another layer, the objects in my array change !!!!
I have debugged the script, and the problems occur after the first move to another layer.
This is the example code...
$.write("------> Script started \n");
var myDoc = app.activeDocument;
var myPageItems = myDoc.pages[0].pageItems;
var myObjectList = new Array();
for (ii = 0; ii < myPageItems.length; ii++) {
if (myPageItems[ii].label != ""){
myObjectList.push(myPageItems[ii]);
$.write("> Pushed : " + myPageItems[ii].label + "\n");
}
}
for (gg = 0; gg < myObjectList.length; gg++) {
myObjectList[gg].itemLayer = app.activeDocument.layers.item("Final");
$.write("> Moved : " + myObjectList[gg].label + "\n");
}
$.write("------> Script stopped \n");
This is the output
------> Script started
> Pushed : Banana
> Pushed : Appel
> Pushed : Lemon
> Moved : Banana
> Moved : Banana
> Moved : Lemon
------> Script stopped
As you can see in the above example, an item with script label "Banana" is only pushed once, but processed twice. The opposite occurs as well. Page Items disappear from the area and are replaced with page items which where not put in the array !!!
Pushing the items to the array works well. All records are put in the list correctly. After the first move, the content of the array changes !!!
The example files contains 10 page items, and only 3 contain a script label.
Does anyone have a solution ?
Hi,
here an explanation for what you see:
document.pageItems describes a collection, allPageItems is an array.
A collection can be a "moving target" so to say.
You could transform that collection to an array to get rid of the "moving target" problem by resolving the page items:
document.pageItems.everyItem().getElements() will return an array.
Recommended read by Marc Autret:
Indiscripts :: On ‘everyItem()’ – Part 1
Indiscripts :: On ‘everyItem()’ – Part 2
Regards,
Uwe
Copy link to clipboard
Copied
I found a workaround. By starting over after every change, It will do the job, but is significant slower when processing e few 100 page items. Does anyone have a solution ?
$.write("------> Script started \n");
var myDoc = app.activeDocument;
var myPageItems = myDoc.pages[0].pageItems;
var doProcess = true;
while (doProcess){
var myList = getList();
for (gg = 0; gg < myList.length; gg++) {
if (myList[gg].itemLayer.name != "Final"){
myList[gg].itemLayer = app.activeDocument.layers.item("Final");
$.write("> Moved : " + myList[gg].label + "\n");
if (gg == myList.length -1){
doProcess = false;
}
break;
}
}
}
function getList(){
var myObjectList = new Array();
for (ii = 0; ii < myPageItems.length; ii++) {
if (myPageItems[ii].label != ""){
myObjectList.push(myPageItems[ii]);
$.write("> Pushed : " + myPageItems[ii].label + "\n");
}
}
return myObjectList;
}
$.write("------> Script stopped \n");
Copy link to clipboard
Copied
It seems there is a huge difference in using .pageItems and .allPageItems.
At first, they seem identical. But they behave differently.
Using the code below, will fix the problem...
var myPageItems = myDoc.pages[0].allPageItems;
Copy link to clipboard
Copied
Just one small comment
The normal practice is to use $.writeln(foo) and not $.write(foo + '\n')
Copy link to clipboard
Copied
Hi,
here an explanation for what you see:
document.pageItems describes a collection, allPageItems is an array.
A collection can be a "moving target" so to say.
You could transform that collection to an array to get rid of the "moving target" problem by resolving the page items:
document.pageItems.everyItem().getElements() will return an array.
Recommended read by Marc Autret:
Indiscripts :: On ‘everyItem()’ – Part 1
Indiscripts :: On ‘everyItem()’ – Part 2
Regards,
Uwe
Copy link to clipboard
Copied
Hi Uwe,
Thank you for your feedback. The links are very usefull.
Kind regards