Recently I had needed to duplicate single smart object layer into 100s of Layer Sets. So I decided to write a script for it, enjoy…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
// Duplicate Selected Layer which is not under Layer Set to Selected Layer Sets (Mehmet Sensoy) #target photoshop var scriptName = "Duplicate Layer into Selected Layer Groups"; var scriptVersion = "-0.2"; cTID = function(s) { return app.charIDToTypeID(s); }; sTID = function(s) { return app.stringIDToTypeID(s); }; var doc = app.activeDocument; var layers = doc.artLayers; var SelectedLayer = null; var resultLayers=[]; var selectedGroupNames = []; var selectedGroupIds= []; var run = true; function optionsDialog() { // Options Dialog var ButtonWidth = 100; OpenOptionsDialog = new Window("dialog", scriptName + " v" + scriptVersion); OpenOptionsDialog.orientation = 'column'; OpenOptionsDialog.alignChildren = 'left'; mainGroup = OpenOptionsDialog.add("group"); mainGroup.orientation = 'column'; mainGroup.alignChildren = 'left'; mainGroup.alignment = 'left'; var layerNamesDropdown = mainGroup.add("dropdownlist",undefined) layerNamesDropdown.preferredSize.width = 250; for (var z = 0; z < layers.length; z++) { layerNamesDropdown.add("item", layers[z].name); } layerNamesDropdown.selection = 0; mainGroup.add("statictext", undefined, ""); ButtonGroup = mainGroup.add("group"); ButtonGroup.orientation = 'row'; ButtonGroup.alignChildren = 'center'; ButtonGroup.alignment = 'top'; // Run Button buttonRun= ButtonGroup.add("button",undefined, "Run") buttonRun.preferredSize.width = ButtonWidth; buttonRun.onClick = function() { layerSelection = layerNamesDropdown.selection.text; SelectedLayer = layerSelection; OpenOptionsDialog.close(); return } // Close Button buttonClose= ButtonGroup.add("button",undefined, "Exit") buttonClose.preferredSize.width = ButtonWidth; buttonClose.onClick = function() {OpenOptionsDialog.close(); run = false; } OpenOptionsDialog.center(); OpenOptionsDialog.show(); } function Select_Layer(layerName) { if(run){ var desc1 = new ActionDescriptor(); var ref1 = new ActionReference(); ref1.putName(cTID('Lyr '), layerName); desc1.putReference(cTID('null'), ref1); desc1.putBoolean(cTID('MkVs'), false); var list1 = new ActionList(); list1.putInteger(1); desc1.putList(cTID('LyrI'), list1); executeAction(cTID('slct'), desc1, DialogModes.NO); }}; function selectLayerByIndex(index,add){ add = undefined ? add = false:add var ref = new ActionReference(); ref.putIndex(charIDToTypeID("Lyr "), index); var desc = new ActionDescriptor(); desc.putReference(charIDToTypeID("null"), ref ); if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) ); desc.putBoolean( charIDToTypeID( "MkVs" ), false ); try{ executeAction(charIDToTypeID("slct"), desc, DialogModes.NO ); }catch(e){ alert(e.message); } }; function getSelectedLayersIdx(){ var selectedLayers = new Array; var ref = new ActionReference(); ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); var desc = executeActionGet(ref); if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){ desc = desc.getList( stringIDToTypeID( 'targetLayers' )); var c = desc.count var selectedLayers = new Array(); for(var i=0;i<c;i++){ try{ activeDocument.backgroundLayer; selectedLayers.push( desc.getReference( i ).getIndex() ); }catch(e){ selectedLayers.push( desc.getReference( i ).getIndex()+1 ); } } }else{ var ref = new ActionReference(); ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" )); ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); try{ activeDocument.backgroundLayer; selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1); }catch(e){ selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))); } } return selectedLayers; }; // Main function main() { app.bringToFront(); optionsDialog(); if(run){ // Start var theLayers = getSelectedLayersIdx(); for (var p = 0; p < theLayers.length; p++) { selectLayerByIndex(theLayers[p], false); if (activeDocument.activeLayer.typename == "LayerSet") { theLayers[p] = app.activeDocument.activeLayer; selectedGroupNames.push (theLayers[p].name); selectedGroupIds.push(theLayers[p]); } } // End of Loop for (var i = 0; i < selectedGroupIds.length; i++) { var currentGroup = selectedGroupIds[i]; var currentLayer = Select_Layer(SelectedLayer); currentLayer = activeDocument.activeLayer; var duplicatedLayer = currentLayer.duplicate(); duplicatedLayer.name = currentLayer.name + '_Duplicate'; duplicatedLayer.move(currentGroup, ElementPlacement.INSIDE); duplicatedLayer.move(currentGroup, ElementPlacement.PLACEATEND) }// End of Loop } } |
main();