Skip to main content
Inspiring
December 12, 2022
Answered

Using JavaScript to reposition a group in Illustrator

  • December 12, 2022
  • 1 reply
  • 1445 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
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,

I am trying to use JavaScript to move a selected group on a specified layer to a particular location on the same layer. In the screenshot and PDF that are attached, I would like to move the group with the yellow star up to the upper-left part of the document.

I tried using .position = [x, y], but it doesn't seem to do anything. It doesn't throw an error, though. I also tried to have the script run an Illustrator action to do the same thing, .doScript(action, set name),  but it gives an error saying that it is not a function.

 

Charu Rajput
Charu RajputCorrect answer
Community Expert
December 12, 2022

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.

Best regards