Copy link to clipboard
Copied
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);
}
}
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
...Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Hi Chris,
Many thanks for you reply, it's most helpful and appreciated!
I'll look at reworking the script.
Thanks,
Nik
Copy link to clipboard
Copied
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);
}
}
}
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Multi-select is a parameter of File.openDlg(Prompt,fileFilter,true) default is false…
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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();
Find more inspiration, events, and resources on the new Adobe Community
Explore Now