Skip to main content
Inspiring
December 12, 2022
Answered

Using JavaScript to reposition a group in Illustrator

  • December 12, 2022
  • 1 reply
  • 1460 views

Hello, everyone,

I am trying to write JavaScript that will take a selected group in an active Illustrator document and move it to a particular X/Y position on the artboard.

I have tried using the .position() method, but this doesn't seem to do anything. I have also tried running an existing Illustrator action to reposition the art using .doScript(), but this either doesn't do anything or crashes Illustrator.

 

var alpha = app.activeDocument.layers.getByName("Layer 2");
alpha.hasSelectedArtwork = true;
alpha.doScript("One", "Large Gabarit Actions");
 
or
 
var alpha = app.activeDocument.layers.getByName("Layer 2");
alpha.hasSelectedArtwork = true;
alpha.position = [100, 100];
 
If anyone could help point out what I am doing wrong, it would be greatly appreciated!
This topic has been closed for replies.
Correct answer Charu Rajput

Hi,

Try following

var _group = app.activeDocument.layers['Layer 2'].groupItems[0];
_group.position = [0,0];

 

You need to access the group inside the Layer. In your code, alpha is a layer not group inside the Layer 2. Therefore it does not work.

1 reply

Charu Rajput
Community Expert
Community Expert
December 12, 2022

Hi,

You said you would like reposition group, but you are trying to move Layer 2.

What excatly you want to reposition, selected items? Could you post the screenshot and specify what items you want to position. Attaching ai file will also be helpful

Best regards
Inspiring
December 12, 2022

Hello,

What I would like to do is have the JavaScript take the group item which is on a specified layer and move it on the artboard to a specific xy location.

In the test .ai file I have attached, I would like to take the group item in Layer 2 and move the group to the upper left corner in the document on the same layer.

 

The screenshot shows the group item at the bottom of the screen of the yellow star. I would like to move this whole group to the upper left corner into position on the document.

 

I am guessing that my code is trying to move the layer in the palette rather than the items on the layer.