Skip to main content
ScriptingRules
Known Participant
December 19, 2012
Answered

Move pageItems from one layer to another

  • December 19, 2012
  • 2 replies
  • 1456 views

I am working on a script to standardize layers and I need to move all the pageItems from a layer called "PART_NUMBER" to a layer named "STATIC".

Can someone help me straighten out the following code at the arrow please...

I am using "test" to troubleshoot the move.

standardizeLayerNames()

// This function verifies that the Layer names conform to the standard Layer names.
     function standardizeLayerNames()
     {
                var myDoc7=app.activeDocument
                var myLayerCount7 = myDoc7.layers.length

                for(var myCounter7 = 0; myCounter7 < myLayerCount7; myCounter7++){
                        var selectLayer7 = myDoc7.layers[myCounter7]

                            if(selectLayer7.name.substr(0,11) == "PART_NUMBER"){
                                for(var test = selectLayer7.pageItems.length ; test >= 0 ; test --){
                                var test1 =  selectLayer7.layers.pageItems[test]   //<----------------------------------
                                //selectLayer7.pathItems[myCounter7].move(app.activeDocument.layers["STATIC"], ElementPlacement.PLACEATBEGINNING )
                                }
                            }
                }
     }

This topic has been closed for replies.
Correct answer CarlosCanto
var test1 =  selectLayer7.layers.pageItems[test]   //<------

couple of things, if you have 3 objects, test = 3, but your items indexes are 0,1,2, you have to target your elements 1 less than the count.

var test1 =  selectLayer7.layers.pageItems[test-1]

however, you have an extra layer that needs to be removed

var test1 =  selectLayer7.pageItems[test-1];

2 replies

CarlosCanto
Community Expert
CarlosCantoCommunity ExpertCorrect answer
Community Expert
December 20, 2012
var test1 =  selectLayer7.layers.pageItems[test]   //<------

couple of things, if you have 3 objects, test = 3, but your items indexes are 0,1,2, you have to target your elements 1 less than the count.

var test1 =  selectLayer7.layers.pageItems[test-1]

however, you have an extra layer that needs to be removed

var test1 =  selectLayer7.pageItems[test-1];

ScriptingRules
Known Participant
December 20, 2012

I agree that the line that is commented out needs to be adjusted.

The test1 line is the line is what I will use to adjust the last line.

The error I'm getting is "No Such Element" on the line:

var test1 =  selectLayer7.pageItems[test-1];

I have been having this problem from the get go and it seems like the structure is wrong.

ScriptingRules
Known Participant
December 20, 2012

Ok, after spending a few more seconds troubleshooting, the for statement needed only a greater than symbol instead of >=  the final solution is as follows.  Thanks for the indexing help I appreciate it...

     function standardizeLayerNames()
     {
                var myDoc7=app.activeDocument
                var myLayerCount7 = myDoc7.layers.length

                for(var myCounter7 = 0; myCounter7 < myLayerCount7; myCounter7++){
                        var selectLayer7 = myDoc7.layers[myCounter7]
                            if(selectLayer7.name.substr(0,11) == "PART_NUMBER"){
                                for(var test = selectLayer7.pageItems.length ; test > 0 ; test --){
                                var test1 =  selectLayer7.pageItems[test-1];
                                selectLayer7.pageItems[test-1].move(app.activeDocument.layers["STATIC"], ElementPlacement.PLACEATBEGINNING )
                                }
                            }
                 }
     }

Larry G. Schneider
Community Expert
Community Expert
December 19, 2012

You're mixing pageItems and pathItems in the last couple of lines.