Skip to main content
Inspiring
August 6, 2010
Answered

How do I script a new position for an entire layer?

  • August 6, 2010
  • 2 replies
  • 2696 views

I have an 8x10 template but the artboard needs to be larger than the template's page size. I can open the template and resize the artboard to the larger size but the contents of the original template still need to be centered on the new artboard. The artboard is the right position, all of the original template's objects need to be repositioned.

With the GUI it's easy, just select the entire layer with the template contents and enter a new x,y position. In Javascript there is no position setting for an entire layer as a single unit.

Other alternatives I've tried:

  1. Create a new doc with the right size and use groupItems.createFromFile() for the template - centers the template: strips off the names of all objects and basically makes a hash of the original template. No good since I need those path and group names intact. Why couldn't it just work like Corel's import?
  2. Create a new doc, open the template, use copy and paste to copy template layers into the new document. Slow and uses up lots of memory. It works ok if there's a border around the template page that makes the centering work.It also works for multiple layers and centers the contents of all layers as a single unit.

One thing I can think of is that groupItems can be positioned so I could put everything in the layer into a single group and move that new group and then move everything back out of the group. This doesn't work for multiple layers unless I want to merge them (which I don't).

Any other suggestions on positioning the contents of one or more layers to be centered on the page? Multiple layers would need to keep their original relative positions.

This topic has been closed for replies.
Correct answer Muppet_Mark-QAl63s

I may be misunderstanding or just over simplifying this but don't you just need to measure the center of the visual bounds against the center of the art board then translate all items by the same amount? I don't have multi-artboards nore can I resize then but I do use this…

#target illustrator var docRef = app.activeDocument; with (docRef) {      rulerOrigin = [0,0];      var docVB = visibleBounds;      var transX = (width / 2) - ((docVB[0] + docVB[2]) / 2);      var transY = (height / 2) - ((docVB[1] + docVB[3]) / 2);      for (var i = 0; i < pageItems.length; i++) {           pageItems.translate(transX, transY, true, true, true, true);      } }

2 replies

Known Participant
February 22, 2019

Hi, i tried your code and its awesome, it will change my layers position to the center of the art board. and how can i change the position to top left of the art board? can you please tell me if you know. thanks.

Muppet_Mark-QAl63s
Muppet_Mark-QAl63sCorrect answer
Inspiring
August 7, 2010

I may be misunderstanding or just over simplifying this but don't you just need to measure the center of the visual bounds against the center of the art board then translate all items by the same amount? I don't have multi-artboards nore can I resize then but I do use this…

#target illustrator var docRef = app.activeDocument; with (docRef) {      rulerOrigin = [0,0];      var docVB = visibleBounds;      var transX = (width / 2) - ((docVB[0] + docVB[2]) / 2);      var transY = (height / 2) - ((docVB[1] + docVB[3]) / 2);      for (var i = 0; i < pageItems.length; i++) {           pageItems.translate(transX, transY, true, true, true, true);      } }

Inspiring
August 8, 2010

Thanks for the suggestion Mark; translate makes sense, using delta values to shift everything. Unfortunately, that only seemes to work correctly for PathItems. Groups and CompoundPaths were way off. I'll see if I can figure out why.

Inspiring
August 8, 2010

Additional info:

It works fine if I do one layer at a time, even if there's only one layer. If I use the document.PageItems groups and CompoundPaths get translated twice. Since a layer isn't a PageItem I have no idea why. Maybe it's a bug. I also tried the PageItems in reverse order but that didn't matter.

Apparently document.pageItems includes both compound paths as well as their included component paths separately and also includes GroupItem objects while also including individual items within the group as separate PageItems. Maybe that's a bug. I'm using CS4.

If I either exclude PageItems with a typename of 'CompoundPathItem' and 'GroupItem' from the translate or use the layer.pageItems it works.

    var docRef = app.activeDocument;
    with (docRef) {
        rulerOrigin = [0,0];
        var docVB = visibleBounds;
        var transX = (width / 2) - ((docVB[0] + docVB[2]) / 2);
        var transY = (height / 2) - ((docVB[1] + docVB[3]) / 2);
        for (var i = 0; i < pageItems.length; i++) {
            if(pageItems.typename != 'GroupItem' &&  pageItems.typename != 'CompoundPathItem')
               pageItems.translate(transX, transY, true, true, true, true);
        }
    }

Or this:

    var docRef = app.activeDocument;
    with (docRef) {
        rulerOrigin = [0,0];
        var docVB = visibleBounds;
        var transX = (width / 2) - ((docVB[0] + docVB[2]) / 2);
        var transY = (height / 2) - ((docVB[1] + docVB[3]) / 2);
        for (var i = 0; i < layers.length; i++) {
            for (var j = 0; j < layers.pageItems.length;j++) {
                layers.pageItems.translate(transX, transY, true, false, false, false);
            }
        }
    }

I'll probably stick to the latter since if it's a bug the behavior might change at some point.