Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Using JavaScript to reposition a group in Illustrator

Explorer ,
Dec 12, 2022 Dec 12, 2022

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!
TOPICS
Scripting
1.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Dec 12, 2022 Dec 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.

Translate
Adobe
Community Expert ,
Dec 12, 2022 Dec 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 12, 2022 Dec 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.

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 12, 2022 Dec 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.

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 12, 2022 Dec 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 12, 2022 Dec 12, 2022
LATEST

That worked. Thanks for your help and your explanation!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines