Copy link to clipboard
Copied
Hi everyone,
I’m looking a script that can create one slice for each selected layer and then rename each slice, ideally using the layer’s name.
My goal is:
Since slices can only be created through Action Manager, I’m not sure about the correct approach.
If anyone has an example script or guidance, I would really appreciate it.
Thanks!
Sample
Here is a script based on my previously suggested "A" approach:
/*
Create Slices From Selected Layers Bounds vA1.jsx
Stephen Marsh
vA1.0 - 22nd November 2025
https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-create-and-rename-slices-for-selected-layers/td-p/15603067
*/
#target photoshop
// Single history stage undo
activeDocument.suspendHistory("Create Slices From Selected Layers Bounds", "main()");
function main() {
// Get the selected layers: cour...
Copy link to clipboard
Copied
There's a feature in Photoshop that lets you generate images from your layers when they are named in a particular way and you go to File > Automate > Generator Plugins. The naming convention for the layers also lets you set certain parameters such as quality, size, format, and scaling.
Here's a page that details the naming and the process:
https://helpx.adobe.com/photoshop/using/generate-assets-layers.html
Copy link to clipboard
Copied
@Myra Ferguson Thanks for the help, but what I wanted was to create slices, not export the images. Anyway, thank you very much.
Copy link to clipboard
Copied
You would use the ScriptingListener plugin to record the AM code as a start point.
A couple of ideas:
A) A script to manually create a slice to get the base code, then swap out the recorded placeholder coordinate values with the layer bounds values. This would be run over all selected layers.
or
B) A script to process each selected layer and run the Layer > New Layer Based Slice command
Copy link to clipboard
Copied
Here is a script based on my previously suggested "A" approach:
/*
Create Slices From Selected Layers Bounds vA1.jsx
Stephen Marsh
vA1.0 - 22nd November 2025
https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-create-and-rename-slices-for-selected-layers/td-p/15603067
*/
#target photoshop
// Single history stage undo
activeDocument.suspendHistory("Create Slices From Selected Layers Bounds", "main()");
function main() {
// Get the selected layers: courtesy of jazz-y
var s2t = stringIDToTypeID;
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
var lrs = executeActionGet(r).getList(p),
sel = new ActionReference();
// Loop over the selected layers: courtesy of jazz-y
for (var l = 0; l < lrs.count; l++) {
sel.putIdentifier(s2t('layer'), p = lrs.getReference(l).getIdentifier(s2t('layerID')));
(r = new ActionReference()).putIdentifier(s2t('layer'), p);
(d = new ActionDescriptor()).putReference(s2t("target"), r);
//d.putBoolean(s2t("makeVisible"), false);
executeAction(s2t('select'), d, DialogModes.NO);
///// Start doing stuff to the selected layers /////
//alert(app.activeDocument.activeLayer.name);
createSliceFromActiveLayerBounds();
renameActiveSliceToLayerName();
///// Stop doing stuff to the selected layers /////
}
}
///// Functions /////
function createSliceFromActiveLayerBounds() {
var doc = app.activeDocument;
var layer = doc.activeLayer;
// Ensure pixel units
var originalRuler = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
// Get layer bounds in pixels (redundant, but it can't hurt)
var b = layer.bounds;
var left = b[0].as("px");
var top = b[1].as("px");
var right = b[2].as("px");
var bottom = b[3].as("px");
// Build the slices using AM code
var idMk = charIDToTypeID("Mk ");
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putClass(stringIDToTypeID("slice"));
desc.putReference(charIDToTypeID("null"), ref);
var sliceDesc = new ActionDescriptor();
sliceDesc.putEnumerated(
charIDToTypeID("Type"),
stringIDToTypeID("sliceType"),
stringIDToTypeID("user")
);
var rect = new ActionDescriptor();
rect.putUnitDouble(charIDToTypeID("Top "), charIDToTypeID("#Pxl"), top);
rect.putUnitDouble(charIDToTypeID("Left"), charIDToTypeID("#Pxl"), left);
rect.putUnitDouble(charIDToTypeID("Btom"), charIDToTypeID("#Pxl"), bottom);
rect.putUnitDouble(charIDToTypeID("Rght"), charIDToTypeID("#Pxl"), right);
sliceDesc.putObject(charIDToTypeID("At "), charIDToTypeID("Rctn"), rect);
desc.putObject(charIDToTypeID("Usng"), stringIDToTypeID("slice"), sliceDesc);
executeAction(idMk, desc, DialogModes.NO);
// Restore the original ruler units
app.preferences.rulerUnits = originalRuler;
}
function renameActiveSliceToLayerName() {
var layerName = app.activeDocument.activeLayer.name;
var idsetd = charIDToTypeID("setd");
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putEnumerated(
stringIDToTypeID("slice"),
charIDToTypeID("Ordn"),
charIDToTypeID("Trgt")
);
desc.putReference(charIDToTypeID("null"), ref);
var inner = new ActionDescriptor();
inner.putString(charIDToTypeID("Nm "), layerName);
desc.putObject(
charIDToTypeID("T "),
stringIDToTypeID("slice"),
inner
);
executeAction(idsetd, desc, DialogModes.NO);
}
Copy link to clipboard
Copied
@Stephen Marsh This script, option A, works very well. Even when I manually created a slice, it captured and renamed each selected layer perfectly. I didn't know how to manipulate multiple layers with Action Manager, so thank you very much for the script.
Copy link to clipboard
Copied
@Stephen Marsh This script, option A, works very well. Even when I manually created a slice, it captured and renamed each selected layer perfectly. I didn't know how to manipulate multiple layers with Action Manager, so thank you very much for the script.
By @Delta2487
You're welcome. My preference would be the "B" script as it's simpler/faster, however, both the "A" and "B" version should provide idential results when it comes to the slice bounds (just be careful if using styles/FX such as strokes, glows or shadows as these are not taken into account and extra steps would be required in ExtendScript for the full bounds).
I have reused this basic script code framework for multiple selected layers (thanks to @jazz-y) for many different script requests on the forum, it's just a matter of swapping out the referenced functions in the section clearly commented to "do stuff to selected layers". As a bonus this script also offers a single undo history step.
Copy link to clipboard
Copied
@Stephen Marsh I'll keep that in mind, if necessary, I'll convert it into a smart object.
It would be fantastic if script B could be improved by adding a message to stop the process if a slice has already been created on a selected layer. Perhaps I'm asking too much, thank you very much.
Copy link to clipboard
Copied
I don't think that there's any direct relationship between a layer and slices. Code would need to compare layer bounds to the slice coordinates. Seems like a lot of busy work.
Perhaps just wrap the function code or function call in a try/catch block to suppress the error.
Or perhaps the script could clear all slices before use?
Copy link to clipboard
Copied
@Delta2487 - This alternative script uses my "B" version suggestion:
/*
Create Slices From Selected Layers vB1.jsx
Stephen Marsh
vB1.0 - 22nd November 2025
https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-create-and-rename-slices-for-selected-layers/td-p/15603067
*/
#target photoshop
// Single history stage undo
activeDocument.suspendHistory("Create Slices From Selected Layers", "main()");
function main() {
// Get the selected layers: courtesy of jazz-y
var s2t = stringIDToTypeID;
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
var lrs = executeActionGet(r).getList(p),
sel = new ActionReference();
// Loop over the selected layers: courtesy of jazz-y
for (var l = 0; l < lrs.count; l++) {
sel.putIdentifier(s2t('layer'), p = lrs.getReference(l).getIdentifier(s2t('layerID')));
(r = new ActionReference()).putIdentifier(s2t('layer'), p);
(d = new ActionDescriptor()).putReference(s2t("target"), r);
//d.putBoolean(s2t("makeVisible"), false);
executeAction(s2t('select'), d, DialogModes.NO);
///// Start doing stuff to the selected layers /////
//alert(app.activeDocument.activeLayer.name);
newLayerBasedSlice();
renameActiveSliceToLayerName();
///// Stop doing stuff to the selected layers /////
}
}
///// Functions /////
function newLayerBasedSlice() {
var idMk = charIDToTypeID("Mk ");
var desc1305 = new ActionDescriptor();
var idnull = charIDToTypeID("null");
var ref126 = new ActionReference();
var idslice = stringIDToTypeID("slice");
ref126.putClass(idslice);
desc1305.putReference(idnull, ref126);
var idUsng = charIDToTypeID("Usng");
var desc1306 = new ActionDescriptor();
var idType = charIDToTypeID("Type");
var idsliceType = stringIDToTypeID("sliceType");
var idLyr = charIDToTypeID("Lyr ");
desc1306.putEnumerated(idType, idsliceType, idLyr);
var idLyr = charIDToTypeID("Lyr ");
var ref127 = new ActionReference();
var idLyr = charIDToTypeID("Lyr ");
var idOrdn = charIDToTypeID("Ordn");
var idTrgt = charIDToTypeID("Trgt");
ref127.putEnumerated(idLyr, idOrdn, idTrgt);
desc1306.putReference(idLyr, ref127);
var idslice = stringIDToTypeID("slice");
desc1305.putObject(idUsng, idslice, desc1306);
executeAction(idMk, desc1305, DialogModes.NO);
}
function renameActiveSliceToLayerName() {
var layerName = app.activeDocument.activeLayer.name;
var idsetd = charIDToTypeID("setd");
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putEnumerated(
stringIDToTypeID("slice"),
charIDToTypeID("Ordn"),
charIDToTypeID("Trgt")
);
desc.putReference(charIDToTypeID("null"), ref);
var inner = new ActionDescriptor();
inner.putString(charIDToTypeID("Nm "), layerName);
desc.putObject(
charIDToTypeID("T "),
stringIDToTypeID("slice"),
inner
);
executeAction(idsetd, desc, DialogModes.NO);
}
Copy link to clipboard
Copied
@Stephen Marsh This script also works well, but it gave me error 8800 when selecting a layer with a previously created slice, but if it works I will use both, thank you very much.
Copy link to clipboard
Copied
@Stephen Marsh This script also works well, but it gave me error 8800 when selecting a layer with a previously created slice, but if it works I will use both, thank you very much.
By @Delta2487
It creates a new slice from a layer, so that's probably why it errors if a slice already exists. Extra code would be required to check for an existing slice to avoid the error. The expectation is obviously that no slices for the layer should exist.
P.S. I have not been able to reproduce this error.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now