Copy link to clipboard
Copied
I’m looking for a script that will duplicate the selected object or the content of the clipboard into multiple selected layers inside a document.
I have not find any discussions around this, but I’m asking if anyone have already seen such script.
Thanks
Jean-Claude
Jean-Claude try the following one:
1. Should work with a single object, multiple objects, group
2. Should alert a warning, if you select nothing or select some text
3. Will not honor, if layers are hidden or locked
Does it work for you?
...// duplicate on different layers _b02
// by Kai Rübsamen, credits to Hans Haesler
// vorbeugenderweise das Anzeigen von Dialogen aktivieren
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
// prüfen, ob ein Dokument geöffnet ist
i
Copy link to clipboard
Copied
Hi,
"multiple selected layers" could be a main issue here, cause - as far as I know - script is not able to read UI panels states (except active property).
But if you may find other condition rule...
Jarek
Copy link to clipboard
Copied
Just a idea: What if the user select the layers not in the normal panel, instead he click on checkboxes in a dialog?
–Kai
Copy link to clipboard
Copied
Hi Kai,
If so - good condition is found.
Jarek
Copy link to clipboard
Copied
Hi Jarek,
what do you mean with good condition?
I would display a dialog with checkboxes, based on the layers names in the acual document. If the checkboxes are checked, the selected object will be duplicated on these layers.
We had a similiar goal some weeks ago with checked layers in ai-files in our german forum.
–Kai
Copy link to clipboard
Copied
Hi Kai,
I ment the way code could be based on that input and output data are defined definitely.
A dialog with check boxes is a good solution.
Jarek
Copy link to clipboard
Copied
O.K. lets wait what Jean-Claude thinks about a dialog and if he wants to duplicate only single objects, multiple objects, groups …
Copy link to clipboard
Copied
My first idea was indeed a dialog with layers names in it. Much quicker than having to turn on/off visibilities of layers, than return to original stay of document.
For the objects, it could be a single or multiple object or groups.
Copy link to clipboard
Copied
@Jean-Claude – that is all doable, also a "deluxe" version where the dialog is a panel constantly updating the list of layers, if you add and remove, change order or turn on/off visibility.
For your basic need, the following snippet in ExtendScript should be sufficient.
All selected objects will be duplicated to all layers that are visible and not locked:
var mySel = app.selection;
var myLayers = app.documents[0].layers.everyItem().getElements();
for(var s=0;s<mySel.length;s++){
var layerOfSelection = mySel
.itemLayer;
for(var n=0;n<myLayers.length;n++){
if(
myLayers
!== layerOfSelection
&& myLayers
.visible === true && lmyLayers
.locked === false
){
mySel
.duplicate(myLayers); };
};
};
Uwe
Copy link to clipboard
Copied
Hi Guys,
I rewrite Hans Haeslers script a bit. In a first step it will only work with single selected items and will not check, if the selected layers are locked or hidden:
// duplicate on different layers _b01
// vorbeugenderweise das Anzeigen von Dialogen aktivieren
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
// prüfen, ob ein Dokument geöffnet ist
if ( app.documents.length == 0 ) {
alert ( "Open a document" );
exit();
}
// die Auswahl speichern ...
if ( app.selection.length == 1 ) {
var curSel = app.selection[0];
}
else {
alert ( "Warning\rWorks only with one selected item at the moment" );
exit();
}
var curDoc = app.activeDocument;
var allLayers = curDoc.layers;
var nLayers = allLayers.length;
// eventuell vorhandene Dialoge entfernen
try {
app.dialogs.everyItem().destroy();
}
catch (e) {
}
var layerNames = new Array();
for ( var i = 0; i < nLayers; i++ ) {
layerNames.push( allLayers.name );
}
// eventuell vorhandene Dialoge entfernen
try {
app.dialogs.everyItem().destroy();
}
catch (e) {
}
// den Dialog vorbereiten ...
var dlogCheckList = new Array();
var aDialog = app.dialogs.add({ name: "Duplicate object to selected layers", canCancel: true });
with ( aDialog ) {
with ( dialogColumns.add() ) {
with ( dialogRows.add() ) {
staticTexts.add({ staticLabel: "active = duplicate"} );
for ( var n = 0; n < nLayers; n++ ) {
with (dialogRows.add()) {
dlogCheckList.push( checkboxControls.add({ staticLabel: layerNames
})); }
}
}
}
}
// ... anzeigen und die Wahl des Anwenders anwenden
if ( aDialog.show() == true ) {
for ( var k = 0; k < dlogCheckList.length; k++ ) {
var curCheckBox = dlogCheckList
; if ( curCheckBox.checkedState ) {
var curLayer = curDoc.layers.itemByName( layerNames
); curSel.duplicate( curLayer );
}
}
aDialog.destroy();
}
else {
aDialog.destroy();
}
Kai
Copy link to clipboard
Copied
@Uwe Your snippets fail/stop at if( each time. Thanks!
@Kai Wow! This works on a single group object too. Might be enough for my need.
Copy link to clipboard
Copied
Jean-Claude try the following one:
1. Should work with a single object, multiple objects, group
2. Should alert a warning, if you select nothing or select some text
3. Will not honor, if layers are hidden or locked
Does it work for you?
// duplicate on different layers _b02
// by Kai Rübsamen, credits to Hans Haesler
// vorbeugenderweise das Anzeigen von Dialogen aktivieren
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
// prüfen, ob ein Dokument geöffnet ist
if ( app.documents.length == 0 ) {
alert ( "Warning\rOpen a document!" );
exit();
}
// die Auswahl speichern ...
var allSel = app.selection;
var nItems = allSel.length;
// und prüfen
if ( !nItems ) {
alert ( "Warning\rSelect something!" );
exit();
}
if ( nItems == 1 && app.selection[0].hasOwnProperty("baseline") ) {
alert ( "Warning\rDoes not work with text. Select only frames!" );
exit();
}
var curDoc = app.activeDocument;
var allLayers = curDoc.layers;
var nLayers = allLayers.length;
// eventuell vorhandene Dialoge entfernen
try {
app.dialogs.everyItem().destroy();
}
catch (e) {
}
var layerNames = new Array();
for ( var i = 0; i < nLayers; i++ ) {
layerNames.push( allLayers.name );
}
// eventuell vorhandene Dialoge entfernen
try {
app.dialogs.everyItem().destroy();
}
catch (e) {
}
// den Dialog vorbereiten ...
var dlogCheckList = new Array();
var aDialog = app.dialogs.add({ name: "Duplicate object to selected layers", canCancel: true });
with ( aDialog ) {
with ( dialogColumns.add() ) {
with ( dialogRows.add() ) {
staticTexts.add({ staticLabel: "active = duplicate"} );
for ( var n = 0; n < nLayers; n++ ) {
with (dialogRows.add()) {
dlogCheckList.push( checkboxControls.add({ staticLabel: layerNames
})); }
}
}
}
}
// ... anzeigen und die Wahl des Anwenders anwenden
if ( aDialog.show() == true ) {
for ( var k = 0; k < dlogCheckList.length; k++ ) {
var curCheckBox = dlogCheckList
; if ( curCheckBox.checkedState ) {
var curLayer = curDoc.layers.itemByName( layerNames
); for ( var i = nItems-1; i >= 0; i-- ) {
var curSel = allSel;
curSel.duplicate( curLayer );
}
}
}
aDialog.destroy();
}
else {
aDialog.destroy();
}
–Kai
Copy link to clipboard
Copied
@Jean-Claude – sorry, a little typo sneaked in.
I had an additional character in the the variable name of myLayers inside of the if statement.
Here the code again:
var mySel = app.selection;
var myLayers = app.documents[0].layers.everyItem().getElements();
for(var s=0;s<mySel.length;s++){
var layerOfSelection = mySel
.itemLayer;
for(var n=0;n<myLayers.length;n++){
if(
myLayers
!== layerOfSelection
&& myLayers
.visible === true && myLayers
.locked === false
){
mySel
.duplicate(myLayers); };
};
};
Uwe
Copy link to clipboard
Copied
Kai & Uwe, both solutions works perfectly. Both are useful depending on specific need and situation.
Thanks a lot guys for your help and generosity. Hope this can be useful to others too.