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

duplicate layer to different document

Guest
May 21, 2011 May 21, 2011

Hi All,

I'm new to javascript and would like a little help please!!

I recently found a script that creates a new layer in the active document and then duplicates the contents of 'Layer 1' of the active doc to it.

#target illustrator

var docRef = app.activeDocument;

with(docRef) {

     var ln = 'Layer 1'

     var ol = layers.getByName(ln);

     var nl = layers.add();

     nl.name = ln+' Copy';

     for (var a = ol.pageItems.length-1; a >= 0; a--) {

          ol.pageItems.duplicate(nl, ElementPlacement.PLACEATBEGINNING);

     }

}

what I would like to do is duplicate a named layer in Document A into Document B. I'm not sure if duplicate is the right word as I suppose the process would have to be to copy the contents of a named layer in Document A then create a layer in Document B with the same name and paste the contents.
Any help with this would be greatly appreciated.
Many thanks,
Nik

TOPICS
Scripting
6.2K
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

Contributor , May 22, 2011 May 22, 2011

Hi Nik,

use of with() is generally not good, maybe:

http://yuiblog.com/blog/2006/04/11/with-statement-considered-harmful/

without with() it looks like that:

#target illustrator

var docRef = app.activeDocument;

var ln = 'Layer 1';

var ol = docRef.layers.getByName(ln);

var nl = docRef.layers.add();

nl.name = ln+' Copy';

for (var a = ol.pageItems.length-1; a >= 0; a--) {

    ol.pageItems.duplicate(nl, ElementPlacement.PLACEATBEGINNING);

}

the second document is a new document?

var doc2 =app.documents.add();

or yo

...
Translate
Adobe
Contributor ,
May 22, 2011 May 22, 2011

Hi Nik,

use of with() is generally not good, maybe:

http://yuiblog.com/blog/2006/04/11/with-statement-considered-harmful/

without with() it looks like that:

#target illustrator

var docRef = app.activeDocument;

var ln = 'Layer 1';

var ol = docRef.layers.getByName(ln);

var nl = docRef.layers.add();

nl.name = ln+' Copy';

for (var a = ol.pageItems.length-1; a >= 0; a--) {

    ol.pageItems.duplicate(nl, ElementPlacement.PLACEATBEGINNING);

}

the second document is a new document?

var doc2 =app.documents.add();

or you can get it by name just like the layer

or by index

var doc2 = app,documets[ indexnumber  ];

then var nl should be:

var nl = doc2.layers.add();

chris

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
Guest
May 22, 2011 May 22, 2011

Hi Chris,

Many thanks for you reply, it's most helpful and appreciated!

I'll look at reworking the script.

Thanks,

Nik

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 ,
May 22, 2011 May 22, 2011

I took a little too long, here's my version

#target illustrator

var docRef = app.activeDocument;

alert(docRef.name);

var docList = "Enter the Destination Document Index \n \n";

var docCount = app.documents.length;

for (i=0 ; i<docCount ; i++)

     {

          docList = docList + i + " - " + app.documents + "\n";

     }

          

var docIndex = Window.prompt (docList, 0, "Copy layer to same or other document")

if (docIndex == null || docIndex >= docCount)

     {

          alert("Enter a number between 0 and " + (docCount-1) + " and press Enter");          

     }

else

     {

          var destDoc = app.documents[docIndex];

          //alert(destDoc.name);

          with(docRef)

               {

                    var ln = 'Layer 1'

                    var ol = layers.getByName(ln);

                    var nl = destDoc.layers.add();

                    nl.name = ln //+' Copy';

                    for (var a = ol.pageItems.length-1; a >= 0; a--)

                         {

                              ol.pageItems.duplicate(nl, ElementPlacement.PLACEATBEGINNING);

                         }

               }

     }

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
Guest
May 22, 2011 May 22, 2011

Hi Carlos,

I really appreciate you looking and replying too.

In relation to this script does anybody know if you can select multiple files from dialog window?

I know how to select a single file or folder but can't find any javascript reference to selecting multiple files

Thanks again,

Nik

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
Guide ,
May 22, 2011 May 22, 2011

Multi-select is a parameter of File.openDlg(Prompt,fileFilter,true) default is false…

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
Guest
May 22, 2011 May 22, 2011

Hi Mark,

Thanks for that, it works a treat.

One last question, I promise!

I want to display a list for a user to select an item from. I think the ListBox command is what I need but I'm not sure of the syntax. Could somebody point me in the right direction.

Thanks,

Nik

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 ,
May 22, 2011 May 22, 2011
LATEST

here's a usage sample, I don't remember where I got it from to give proper credits.

var myList = ["Item 1","Item 2","Item 3","Item 4","Item 5","Item 6","Item 7","Item 8","Item 9","Item 10"];

var win = new Window('dialog', 'myDlg');

var ddL = win.add('dropdownlist',undefined,myList);

ddL.onChange = function(){alert("Item changed");}

win.center();

win.show();

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