I am writing a script to facilitate my slicing process. What I normally do is double the canvas, duplicate and drag the layers I want to slice onto the new canvas space, and then slice them.
The following script does everything except the last step. I am calling an action to slice the assets, but it's slicing the original layer and not the duplicated layer. Also, the action is creating a slice from layer, so when it runs into a layer set it fails. Any thoughts/suggestions would be appreciated!
Also let me know if you have any suggestions to make this better.
Code: Select all//******************************************
// Move layers for slicing
// Author: Chris Macholz
//
// 1. Doubles the width of the canvas
// 2. Duplicates layers and layerSets (groups) that have //slice appended to their name
// 3. Moves layers into position for slicing and into a folder called _slices
// 4. Creates a layer-based slice around the newly created layers and layer groups
#target photoshop
//global variables
var doc = app.activeDocument;
doc.resizeCanvas(doc.width * 2, doc.height + 0, AnchorPosition.MIDDLELEFT);
scanLayerSets(doc);
function moveLayerToSet(doc, srcLayer, toLayerName) {
var destLayer = doc.layers[toLayerName];
if(destLayer.layers) destLayer = destLayer.layers[0];
srcLayer.move(destLayer, ElementPlacement.PLACEBEFORE);
}
function scanLayerSets(el) {
//find layer groups whose names end with //slice
for(var a=0;a<el.layerSets.length;a++){
var lname = el.layerSets[a].name;
if (lname.substr(-7) == "//slice") {
doc.activeLayer = el.layerSets[a];
var newlname = lname.replace('//slice','');
doc.activeLayer.name = newlname;
var newlayerset = doc.activeLayer.duplicate();
newlayerset.translate( new UnitValue(.5,'%'),undefined);
//set source layerSet to move into _slices folder
var srcLayer = newlayerset;
moveLayerToSet(doc, srcLayer, '_slices');
} else {
// recursive
scanLayerSets(el.layerSets[a]);
}
}
// find plain layers in current group whose names end with //slice
for(var j=0; j<el.artLayers.length; j++) {
var name = el.artLayers[j].name;
if (name.substr(-7) == "//slice") {
var layer = el.layers.getByName(name);
doc.activeLayer = el.layers[j];
var newname = name.replace('//slice','');
doc.activeLayer.name = newname;
var newlayer = doc.activeLayer.duplicate();
newlayer.translate( new UnitValue(.5,'%'),undefined);
//newlayer.move(doc.layers.getByName('_slices'), ElementPlacement.INSIDE);
//set source layer to move into _slices folder
var srcLayer = newlayer;
moveLayerToSet(doc, srcLayer, '_slices');
// doAction('Make Slice From Layer', 'Bjango Actions'); //Calls an action that makes the slice from the current layer, but it's placing the slice over the old layer
}
}
}
alert("Operation Complete!");