Skip to main content
Known Participant
September 19, 2021
Answered

Move the selected layer to new artboard

  • September 19, 2021
  • 1 reply
  • 1419 views

Hi All,

 

Could anyone helpme how to move the specific layer to new artboard via script. I'm newbie to illustrator scripting.


For Example, in artboard 1 have layer1, layer 2, layer3. I want to create the new artboard and move the layer 2 and layer 3 to new artboard from artboard 1. 

 

 

 

 

This topic has been closed for replies.
Correct answer Charu Rajput

I have modified the code based on the layer name but i got error. Could you please where I did wrong in the code.

var gout = 10;
var dx, dy;
    dx = dy = 0;
var docRef = app.activeDocument;
var docLayer = docRef.layers
var ab = docRef.artboards[0];
var rect = ab.artboardRect;
var W = docRef.width; // or docRef.width;
var H = docRef.height; // or docREf.height;
    dx = W+gout;
    ab = docRef.artboards.add([rect[0]+dx,rect[0]+dy,rect[0]+dx+W,rect[1]-H+dy]);
      for ( var p = 0 ; p < docLayer.length ; p++){
        docLayer[p].name("White").translate(dx,dy);
      }

Hi,

You are using translate method on layer, but translate method does not exists on the layers. So here is what you need to do

1. Get layer by name.

2. Get pageItems of that layer

3. Use translate method on page items.

 

You can try the following snippet that will move the item of layer named as "White" to new artboard.

var gout = 10;
var dx, dy;
dx = dy = 0;
var docRef = app.activeDocument;
var docLayers = docRef.layers
var ab = docRef.artboards[0];
var rect = ab.artboardRect;
var W = docRef.width; // or docRef.width;
var H = docRef.height; // or docREf.height;
dx = W + gout;
ab = docRef.artboards.add([rect[0] + dx, rect[0] + dy, rect[0] + dx + W, rect[1] - H + dy]);
var _pageItems = docLayers["White"].pageItems;
for (var p = 0; p < _pageItems.length; p++) {
    _pageItems[p].translate(dx, dy);
}

 

1 reply

renél80416020
Inspiring
September 20, 2021

Bonour,

Sélectionnez les calques à déplacer

 

  var gout = 10;
  var dx, dy;
      dx = dy = 0;
  var docRef = app.activeDocument;
  var ab = docRef.artboards[0];
  var rect = ab.artboardRect;
  var W = rect[2]-rect[0]; // or docRef.width;
  var H = rect[1]-rect[3]; // or docREf.height;
      dx = W+gout;
      ab = docRef.artboards.add([rect[0]+dx,rect[0]+dy,rect[0]+dx+W,rect[1]-H+dy]);
        for ( var p = 0 ; p < selection.length ; p++){
          selection[p].translate(dx,dy);
        }

 

.René

Rocky@Author
Known Participant
September 21, 2021

Hi Rene,

 

Thanks for the code. is it possible to move the layer based on the name without the selection of layer?

Charu Rajput
Community Expert
Charu RajputCommunity ExpertCorrect answer
Community Expert
September 21, 2021

I have modified the code based on the layer name but i got error. Could you please where I did wrong in the code.

var gout = 10;
var dx, dy;
    dx = dy = 0;
var docRef = app.activeDocument;
var docLayer = docRef.layers
var ab = docRef.artboards[0];
var rect = ab.artboardRect;
var W = docRef.width; // or docRef.width;
var H = docRef.height; // or docREf.height;
    dx = W+gout;
    ab = docRef.artboards.add([rect[0]+dx,rect[0]+dy,rect[0]+dx+W,rect[1]-H+dy]);
      for ( var p = 0 ; p < docLayer.length ; p++){
        docLayer[p].name("White").translate(dx,dy);
      }

Hi,

You are using translate method on layer, but translate method does not exists on the layers. So here is what you need to do

1. Get layer by name.

2. Get pageItems of that layer

3. Use translate method on page items.

 

You can try the following snippet that will move the item of layer named as "White" to new artboard.

var gout = 10;
var dx, dy;
dx = dy = 0;
var docRef = app.activeDocument;
var docLayers = docRef.layers
var ab = docRef.artboards[0];
var rect = ab.artboardRect;
var W = docRef.width; // or docRef.width;
var H = docRef.height; // or docREf.height;
dx = W + gout;
ab = docRef.artboards.add([rect[0] + dx, rect[0] + dy, rect[0] + dx + W, rect[1] - H + dy]);
var _pageItems = docLayers["White"].pageItems;
for (var p = 0; p < _pageItems.length; p++) {
    _pageItems[p].translate(dx, dy);
}

 

Best regards