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

Place a layer in specific area on new artboard

Contributor ,
Mar 16, 2020 Mar 16, 2020

Copy link to clipboard

Copied

I currently duplicate the contents of one document into another like so:

  // add selected to document
  if( docSelected.length > 0 ) {
    // move items to selected document
    // var newDoc = aDoc.documents.add();
    if( docSelected.length > 0 ) {
      for ( i = 0; i < docSelected.length; i++ ) {
        docSelected[i].selected = false;
        newItem = docSelected[i].duplicate( aDoc, ElementPlacement.PLACEATEND );
      }
    }
    else {
      docSelected.selected = false;
      newItem = docSelected.parent.duplicate( aDoc,
      ElementPlacement.PLACEATEND );
    }
  }

aDoc is the file where the content goes into.

All works.

After I place it or before (doesn't matter) I'd like to palce it at a specific area of the board.  In example

top = 0, left = 0.

My attempt:

  var activeLayer = aDoc.artLayers.getByName("Layer 1");
  activeLayer.top = 0;
  activeLayer.left = 0;

 Didn't work.  Any ideas how I could accomplish this?

TOPICS
Scripting

Views

828

Translate

Translate

Report

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 , Mar 17, 2020 Mar 17, 2020

the Layer object doesn't have "left" or "top" properties. You need to target the artboard directly to get the coordinates. Consider this very simple example:

#target Illustrator

//Description: 
function test()
{
	var doc = app.activeDocument;
	var item = doc.pageItems[0];
	var ab = app.activeDocument.artboards[0];
	var rect = ab.artboardRect;

	item.left = rect[0]; // Artboard.artboardRect[0] refers to the left edge of the artboard
	item.top = rect[1]; // Artboard.artboardRect[0] refers to the l
...

Votes

Translate

Translate
Adobe
Community Expert ,
Mar 17, 2020 Mar 17, 2020

Copy link to clipboard

Copied

the Layer object doesn't have "left" or "top" properties. You need to target the artboard directly to get the coordinates. Consider this very simple example:

#target Illustrator

//Description: 
function test()
{
	var doc = app.activeDocument;
	var item = doc.pageItems[0];
	var ab = app.activeDocument.artboards[0];
	var rect = ab.artboardRect;

	item.left = rect[0]; // Artboard.artboardRect[0] refers to the left edge of the artboard
	item.top = rect[1]; // Artboard.artboardRect[0] refers to the left edge of the artboard  
	
}
test();

 

Votes

Translate

Translate

Report

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
Contributor ,
Mar 17, 2020 Mar 17, 2020

Copy link to clipboard

Copied

Thank you so much.  If there's multiple pageItems is there away to group them all at once and then apply the left top or would I have to loop over each one?  @williamadowling

Votes

Translate

Translate

Report

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 ,
Mar 23, 2020 Mar 23, 2020

Copy link to clipboard

Copied

LATEST

when you have all your items selected, you can use this:

app.executeMenuCommand("group");

//do the transformation/alignment stuff

app.executeMenuCommand("ungroup");

Votes

Translate

Translate

Report

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